简体   繁体   English

将数据从一个数据帧提取到 r 中的另一个数据帧

[英]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.

相关问题 从一个数据集中提取数据,在r中使用另一个 - Extracting data from one data set, using another in r 来自一个 dataframe 的 Select 数据,通过与 ZE1E1D3D40573127ED6EE038 中的另一个 dataframe 进行比较 - Select data from one dataframe by comparing against another dataframe in R 从存在于另一个数据帧 R 中的一个数据帧中删除数据 - Removing data from one dataframe that exists in another dataframe R 将数据从一个数据框的列导入到R中的另一数据框? - Importing data from column of one data frame to another dataframe in r? 在 R 中将字符串从一列提取到另一列 - Extracting a string from one column into another in R R:在 dataframe 单元格搜索中插入值,从另一个 dataframe 中提取它 - R: insert value in dataframe cell searching extracting it from another dataframe 从一个 dataframe 中提取变量列表并将其应用于另一个 dataframe - Extracting a list of variables from one dataframe and applying it to another dataframe 如何将数据从一个 dataframe 推算到另一个 r - How to impute data from one dataframe to another in r R:有条件地将数据从一个数据帧提取到另一个 - R: Conditionally extract data from one dataframe to another R-将数据帧中的数据从一个时标转换为另一个时标 - R - Transforming data in dataframe from one time scale to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM