从R中的数据框中排序特定日期

时间:2014-05-02 14:22:04

标签: r

我是R的新手。我在交易中有一个相当大的数据框,需要整理那些仅在周三(现在)发生的数据框。

最好的方法是什么?提前谢谢。

以下是一个小样本

    symbol  t_date  exchange    price   hv10    hv20

      AA    4/25/2011   NYSE    16.89   0.3797  0.3024
      AA    4/26/2011   NYSE    17.031  0.3859  0.3022
      AA    4/27/2011   NYSE    17.18   0.2141  0.2991
      AA    4/28/2011   NYSE    17.09   0.209   0.2974
      AA    4/29/2011   NYSE    17      0.2129  0.2975
      AA    5/2/2011    NYSE    17.22   0.2169  0.2999
      AA    5/3/2011    NYSE    17.67   0.1768  0.3139

2 个答案:

答案 0 :(得分:2)

这是一个循环,让你使用Henrik的建议来获取星期五。

years <- 2011:2013
months <- month.abb

third_fridays_inner <- c()
third_fridays_outer <- c()

for(year in years){
  require(RcppBDT)

  theYear <- year 

  for(month in months){
      third_friday <- 
        getNthDayOfWeek(third, 5, do.call(get, list(month)), theYear)
      third_fridays_inner <- c(third_fridays_inner, third_friday)
  }

  third_fridays_outer <- unique(c(third_fridays_outer, third_fridays_inner))

}

third_fridays <- as.Date(third_fridays_outer, origin = '1970-01-01')
third_fridays

 [1] "2011-01-21" "2011-02-18" "2011-03-18" "2011-04-15" "2011-05-20" "2011-06-17"
 [7] "2011-07-15" "2011-08-19" "2011-09-16" "2011-10-21" "2011-11-18" "2011-12-16"
[13] "2012-01-20" "2012-02-17" "2012-03-16" "2012-04-20" "2012-05-18" "2012-06-15"
[19] "2012-07-20" "2012-08-17" "2012-09-21" "2012-10-19" "2012-11-16" "2012-12-21"
[25] "2013-01-18" "2013-02-15" "2013-03-15" "2013-04-19" "2013-05-17" "2013-06-21"
[31] "2013-07-19" "2013-08-16" "2013-09-20" "2013-10-18" "2013-11-15" "2013-12-20"

然后,您必须转换原始数据集中的日期。

data_set$t_date <- as.Date(data_set$t_date, foramt = '%m/%d/%Y')

然后,您可以将原始数据集过滤到third_fridays。

library(dplyr)
data_set %.% filter(t_date %in% third_fridays)

答案 1 :(得分:1)

您可以在数据框中添加星期几和日期编号。 然后过滤当天=周五和天数是15-21 我非常确定这可以更简洁地完成,但希望这会让你更多地了解正在发生的事情。

#Put the date into ISO format
df$t_date <- as.Date(df$t_date,format="%m/%d/%Y")

#Add in the day abbreviation of week
df$day <- format(df$t_date,"%a")

#Add in the day number of week
df$dayno <- format(df$t_date,"%d")

#filter the data frame to just fridays
df <- df[df$day =="Fri" ,]

#filter the data frame to just the day numbers that occur in the third week
df <- df[(df$dayno>=15 & df$dayno <= 21) ,]