R 重叠观测中不同长度数据帧及其平均值的合并

R 重叠观测中不同长度数据帧及其平均值的合并,r,rbind,R,Rbind,例如,我有3个数据帧: test.df1 date x y z 1 1998-01-01 10 10 10 2 1998-02-01 10 10 10 3 1998-03-01 10 10 10 4 1998-04-01 10 10 10 5 1998-05-01 10 10 10 6 1998-06-01 10 10 10 test.df2 date x y z 1 1998-03-01 5 5 5 2 1998-04-01 5 5 5 3 1998-0

例如,我有3个数据帧:

test.df1
        date  x  y  z
1 1998-01-01 10 10 10
2 1998-02-01 10 10 10
3 1998-03-01 10 10 10
4 1998-04-01 10 10 10
5 1998-05-01 10 10 10
6 1998-06-01 10 10 10

test.df2
        date x y z
1 1998-03-01 5 5 5
2 1998-04-01 5 5 5
3 1998-05-01 5 5 5
4 1998-06-01 5 5 5

test.df3
        date x y z
1 1998-05-01 1 1 1
2 1998-06-01 1 1 1
我想将它们合并在一起,这样新数据帧的行数将与最大数据帧中的行数相同(
test.df1
,在本例中),并且当日期重叠时,变量的平均值将添加到新数据帧中。在上面的示例中,新数据框应该有
4列和
6行

1998-01-01
1998-02-01
x
y
z
应保持
10

对于
1998-03-01
1998-06-01
应为
7.5
(平均值为10+5)

对于
1998-05-01
1998-06-01
,应为
5.33
(10+5+1的平均值)

有没有办法在r中实现这一点

dput(test.df1)
structure(list(date = structure(c(10227, 10258, 10286, 10317, 
10347, 10378), class = "Date"), x = c(10, 10, 10, 10, 10, 10), 
y = c(10, 10, 10, 10, 10, 10), z = c(10, 10, 10, 10, 10, 
10)), .Names = c("date", "x", "y", "z"), row.names = c(NA, 
-6L), class = "data.frame")

dput(test.df2)
structure(list(date = structure(c(10286, 10317, 10347, 10378), class = "Date"), 
x = c(5, 5, 5, 5), y = c(5, 5, 5, 5), z = c(5, 5, 5, 5)), .Names = c("date", 
"x", "y", "z"), row.names = c(NA, -4L), class = "data.frame")

dput(test.df3)
structure(list(date = structure(c(10347, 10378), class = "Date"), 
x = c(1, 1), y = c(1, 1), z = c(1, 1)), .Names = c("date", 
"x", "y", "z"), row.names = c(NA, -2L), class = "data.frame")

我的方法是首先用副本绑定数据帧,然后使用dplyr包(在执行colMeans时,请确保排除非数字列):


我们可以使用
dplyr
tidyr

library(dplyr)
library(tidyr)
test.df1 %>% left_join(test.df2, by = "date") %>%
             left_join(test.df3, by = "date") %>%
             gather(var, val, -date) %>%
             mutate(var = substr(var, 1, 1)) %>%
             group_by(date, var) %>%
             summarise(val = mean(val, na.rm = TRUE)) %>%
             spread(var, val)
Source: local data frame [6 x 4]

        date         x         y         z
      (date)     (dbl)     (dbl)     (dbl)
1 1998-01-01 10.000000 10.000000 10.000000
2 1998-02-01 10.000000 10.000000 10.000000
3 1998-03-01  7.500000  7.500000  7.500000
4 1998-04-01  7.500000  7.500000  7.500000
5 1998-05-01  5.333333  5.333333  5.333333
6 1998-06-01  5.333333  5.333333  5.333333

在R底有一个班轮可以到达:

aggregate(. ~ date, data=rbind(test.df1,test.df2,test.df3), FUN=mean)
#        date         x         y         z
#1 1998-01-01 10.000000 10.000000 10.000000
#2 1998-02-01 10.000000 10.000000 10.000000
#3 1998-03-01  7.500000  7.500000  7.500000
#4 1998-04-01  7.500000  7.500000  7.500000
#5 1998-05-01  5.333333  5.333333  5.333333
#6 1998-06-01  5.333333  5.333333  5.333333
制作一个大的
数据。使用
rbind
对所有行进行帧
,然后按日期进行
聚合
,以便在有重叠时计算平均值

如果您是
dplyr
用户,可以应用相同的逻辑:

library(dplyr)    
rbind_all(list(test.df1,test.df2,test.df3)) %>% 
  group_by(date) %>% 
  summarise_each(funs(mean))

这是可以做到的,但我能想到的唯一方法是相当复杂的。我将查询每个数据帧元素以获得匹配的日期字符串字段,并根据所有匹配的平均值构造一个新的数据帧。答:是的,这可以在R中完成。
aggregate(. ~ date, data=rbind(test.df1,test.df2,test.df3), FUN=mean)
#        date         x         y         z
#1 1998-01-01 10.000000 10.000000 10.000000
#2 1998-02-01 10.000000 10.000000 10.000000
#3 1998-03-01  7.500000  7.500000  7.500000
#4 1998-04-01  7.500000  7.500000  7.500000
#5 1998-05-01  5.333333  5.333333  5.333333
#6 1998-06-01  5.333333  5.333333  5.333333
library(dplyr)    
rbind_all(list(test.df1,test.df2,test.df3)) %>% 
  group_by(date) %>% 
  summarise_each(funs(mean))