从R中的数据框中排序特定日期
我对R非常陌生。我有一个相当大的交易数据框架,需要整理那些只在第三个星期五发生的交易(目前) 最好的方法是什么?先谢谢你 下面是一个小样本从R中的数据框中排序特定日期,r,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
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
可以在数据框中添加星期几,然后添加天数。 然后过滤日期=周五,日期为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) ,]
#将日期转换为ISO格式
df$t_date您可以在数据框中添加一周中的几天,然后添加天数。
然后过滤日期=周五,日期为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) ,]
#将日期转换为ISO格式
df$t_date您可以在数据框中添加一周中的几天,然后添加天数。
然后过滤日期=周五,日期为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) ,]
#将日期转换为ISO格式
df$t_date您可以在数据框中添加一周中的几天,然后添加天数。
然后过滤日期=周五,日期为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) ,]
#将日期转换为ISO格式
df$t_date这里有一个循环,使用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"
这里有一个循环,让你使用亨里克的建议周五
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"
这里有一个循环,让你使用亨里克的建议周五
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"
这里有一个循环,让你使用亨里克的建议周五
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"
可能重复@Henrik no。目标是编写包含所有相关信息的新数据集。不仅仅是根据Henrik的建议计算日期,它是数据集[t_date==getNthDayOfWeek(2011年4月3日,星期五),]
@maloneypatr。有36个月的信息。上述内容仅涉及@Henrik no.的一个月可能的副本。目标是编写一个包含所有相关信息的新数据集。不仅仅是根据Henrik的建议计算日期,它是数据集[t_date==getNthDayOfWeek(2011年4月3日,星期五),]
@maloneypatr。有36个月的信息。上述内容仅涉及@Henrik no.的一个月可能的副本。目标是编写一个包含所有相关信息的新数据集。不仅仅是根据Henrik的建议计算日期,它是数据集[t_date==getNthDayOfWeek(2011年4月3日,星期五),]
@maloneypatr。有36个月的信息。上述内容仅涉及@Henrik no.的一个月可能的副本。目标是编写一个包含所有相关信息的新数据集。不仅仅是根据Henrik的建议计算日期,它是数据集[t_date==getNthDayOfWeek(2011年4月3日,星期五),]
@maloneypatr。有36个月的信息。以上内容只涉及一个月。如何从列表中筛选出两个连续日期之间的数据块。例如:在第一个日期和第二个日期之间,然后将其写入单独的数据帧,然后是第二个和第三个,依此类推嘿,Alex-很抱歉延迟太久…因为我刚刚看到了这一点。我将研究cut
函数。类似于:cut(日期,休息=第三个星期五)
。您可能必须将日期转换为数字,但我认为该函数捕获了逻辑。如何从列表中筛选出两个连续日期之间的数据块。例如:在第一个日期和第二个日期之间,然后将其写入单独的数据帧,然后是第二个和第三个,依此类推嘿,Alex-很抱歉延迟太久…因为我刚刚看到了这一点。我将研究cut
函数。类似于:cut(日期,休息=第三个星期五)
。您可能必须将日期转换为数字,但我认为该函数捕获了逻辑。如何从列表中筛选出两个连续日期之间的数据块。例如:在第一个日期和第二个日期之间,然后将其写入单独的数据帧,然后是第二个和第三个,依此类推嘿,Alex-很抱歉延迟太久…因为我刚刚看到了这一点。我将研究cut
函数。类似于:cut(日期,休息=第三个星期五)
。您可能必须将日期转换为数字,但我认为该函数捕获了逻辑。如何从列表中筛选出两个连续日期之间的数据块。例如:在第一个日期和第二个日期之间,然后将其写入单独的数据帧,然后是第二个和第三个,依此类推嘿,Alex-很抱歉延迟太久…因为我刚刚看到了这一点。我将研究cut
函数。类似于:cut(日期,休息=第三个星期五)
。您可能需要将日期转换为数字,但我认为该函数捕获了逻辑。