将不同长度的数据帧与重叠观察中的平均值合并

时间:2015-11-12 22:10:24

标签: 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-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行。

xyz 1998-01-011998-02-01应保持10;

1998-03-011998-06-01

应为7.5(平均值为10 + 5);

1998-05-011998-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")

3 个答案:

答案 0 :(得分:2)

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

library(plyr)
test.merge <- rbind(test.df1, test.df2, test.df3)
test.merge <- ddply(test.merge, ~date, function(x){
  colMeans(x[,-1])
})

<强>输出

        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

答案 1 :(得分:1)

基地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

使用data.frame制作一个包含所有行的大rbind,然后按日期aggregate制作一个dplyr,以便在存在重叠时计算平均值。

如果您是library(dplyr) rbind_all(list(test.df1,test.df2,test.df3)) %>% group_by(date) %>% summarise_each(funs(mean)) 用户,则可以应用相同的逻辑:

{{1}}

答案 2 :(得分:0)

我们可以使用dplyrtidyr

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