[英]Extracting data from one dataframe to another in r
I have a dataframe containing daily prices of a stock exchange for several years with their respective dates.我有一个数据框,其中包含几年来证券交易所的每日价格及其各自的日期。 I would like to extract the last 3 observations in a month and the first 5 observations of the following month, for every month, and store it in a new dataframe.我想每个月提取一个月中的最后 3 个观察结果和下个月的前 5 个观察结果,并将其存储在一个新的数据框中。
In addition to dates (formated as "%Y-%m-%d") I have a column with a counter for every trading day pr month.除了日期(格式为“%Y-%m-%d”)之外,我还有一列,每个交易日都有一个计数器。 Example data looks like this:示例数据如下所示:
df$date <- as.Date(c("2017-03-25","2017-03-26","2017-03-27","2017-03-29","2017-03-30",
"2017-03-31","2017-04-03","2017-04-04","2017-04-05","2017-04-06",
"2017-04-07","2017-04-08","2017-04-09"))
df$DayofMonth <- c(18,19,20,21,22,23,1,2,3,4,5,6,7)
df$price <- (100, 100.53, 101.3 ,100.94, 101.42, 101.40, 101.85, 102, 101.9, 102, 102.31, 102.1, 102.23)
And now I want to extract the last 3 observations in March and the first 5 observations in April (and then the last 3 observations in April and the first 5 in May etc, including all columns of the respective rows) and store it in a new dataframe.现在我想提取 3 月份的最后 3 个观察结果和 4 月份的前 5 个观察结果(然后是 4 月份的最后 3 个观察结果和 5 月份的前 5 个观察值等,包括相应行的所有列)并将其存储在一个新的数据框。 The only question is how do I do this?唯一的问题是我该怎么做?
Thanks for helping out!感谢您的帮助!
First idea:第一个想法:
date <- c("2017-03-25","2017-03-26","2017-03-27","2017-03-29","2017-03-30",
"2017-03-31","2017-04-03","2017-04-04","2017-04-05","2017-04-06",
"2017-04-07","2017-04-08","2017-04-09")
df <- data.table(Date = date)
df[,YearMonth:=str_sub(Date,1,7)]
df[, DayofMonth := seq(.N), by = YearMonth]
first <- df[, .SD[1:ifelse(.N < 5, .N, 5)], by = YearMonth] #first trading days each month
last <- df[, .SD[(ifelse((.N-2) < 0, 0, (.N-2))):.N], by = YearMonth] #last trading days each month
final <- rbind(first, last)
setorder(final, Date)
# be aware that it leads to duplicates for a month if it has less than 8 trading days,
# to resolve that use unique()
final <- unique(final)
quick and dirty: add a column that is like the DayofMonth column, but shifted by 3 downwards快速而肮脏:添加一个类似于 DayofMonth 列的列,但向下移动了 3
df$dom2 <- df$DayofMonth[4:(nrow(df)+3)]
subset(df, DayofMonth<=5 | dom2<=3)
the only reason we are still filtering with the actual DayofMonth column (instead of jsut saying dom2<=8) is that at the end of dom2 there will be an NA for your example.我们仍然使用实际的 DayofMonth 列(而不是说 dom2<=8)进行过滤的唯一原因是,在 dom2 的末尾将有一个 NA 用于您的示例。 Don't know how your real data looks like, but better safe than sorry.不知道您的真实数据如何,但安全总比抱歉好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.