根据R中第二个数据框中的日期范围对数据框中的数据进行分组
我有以下两个数据帧:根据R中第二个数据框中的日期范围对数据框中的数据进行分组,r,R,我有以下两个数据帧: Date <- seq(as.Date("2013/1/1"), by = "day", length.out = 46) x <-data.frame(Date) x$discharge <- c("1000","1100","1200","1300","1400","1200","1300","1300","1200","1100","1200","1200","1100","1400","1200","1100","1400","1000","110
Date <- seq(as.Date("2013/1/1"), by = "day", length.out = 46)
x <-data.frame(Date)
x$discharge <- c("1000","1100","1200","1300","1400","1200","1300","1300","1200","1100","1200","1200","1100","1400","1200","1100","1400","1000","1100","1200","1300","1400","1200","1300","1300","1200","1100","1200","1200","1100","1400","1200","1100","1400","1000","1100","1200","1300","1400","1200","1300","1300","1200","1100","1200","1200")
x$discharge <- as.numeric(x$discharge)
但是,如果您在数据框中检查范围,2013-01-01到2013-01-07的范围将扩展到2013-01-14,但我只需要它到2013-01-07,然后在下一个范围从2013-01-15开始之前暂停。您可以尝试
tidyverse
library(tidyverse)
y %>%
split(seq_along(1:nrow(.))) %>%
map(~filter(x, between(Date, .$Date_from, .$Date_to)) %>%
summarise(Mean=mean(discharge))) %>%
bind_rows() %>%
bind_cols(y,.)
Date_from Date_to concentration Mean
1 2013-01-01 2013-01-07 1.5 1214.286
2 2013-01-15 2013-01-20 2.5 1166.667
3 2013-01-21 2013-01-25 1.5 1300.000
4 2013-02-10 2013-02-15 3.5 1216.667
仅使用此代码可以查看值和组
y %>%
split(seq_along(1:nrow(.))) %>%
map(~filter(x, between(Date, .$Date_from, .$Date_to)))
下面是一个
base
答案:
helper <- merge(x, y)
helper <- helper[helper$Date >= helper$Date_from & helper$Date <= helper$Date_to, ]
aggregate(helper$discharge,
list(Date_from = helper$Date_from,
Date_to = helper$Date_to),
FUN = 'mean')
Date_from Date_to x
1 2013-01-01 2013-01-07 1214.286
2 2013-01-15 2013-01-20 1166.667
3 2013-01-21 2013-01-25 1300.000
4 2013-02-10 2013-02-15 1216.667
helper抱歉,我无法安装tidyverse。R似乎在依赖项方面存在一些问题。我建议使用RStudio和最新的R版本3.4.1。
y %>%
split(seq_along(1:nrow(.))) %>%
map(~filter(x, between(Date, .$Date_from, .$Date_to)))
helper <- merge(x, y)
helper <- helper[helper$Date >= helper$Date_from & helper$Date <= helper$Date_to, ]
aggregate(helper$discharge,
list(Date_from = helper$Date_from,
Date_to = helper$Date_to),
FUN = 'mean')
Date_from Date_to x
1 2013-01-01 2013-01-07 1214.286
2 2013-01-15 2013-01-20 1166.667
3 2013-01-21 2013-01-25 1300.000
4 2013-02-10 2013-02-15 1216.667