R 如何合并和求和两个数据帧

R 如何合并和求和两个数据帧,r,R,这是我的问题: df1 <- data.frame(x = 1:5, y = 2:6, z = 3:7) rownames(df1) <- LETTERS[1:5] df1 x y z A 1 2 3 B 2 3 4 C 3 4 5 D 4 5 6 E 5 6 7 df2 <- data.frame(x = 1:5, y = 2:6, z = 3:7) rownames(df2) <- LETTERS[3:7] df2 x y z C 1 2 3 D 2 3 4

这是我的问题:

df1 <- data.frame(x = 1:5, y = 2:6, z = 3:7)
rownames(df1) <- LETTERS[1:5]
df1
  x y z
A 1 2 3
B 2 3 4
C 3 4 5
D 4 5 6
E 5 6 7

df2 <- data.frame(x = 1:5, y = 2:6, z = 3:7)
rownames(df2) <- LETTERS[3:7]
df2
  x y z
C 1 2 3
D 2 3 4
E 3 4 5
F 4 5 6
G 5 6 7
其中重复的行由同一变量相加。

使用dplyr

library(dplyr)

# add rownames as a column in each data.frame and bind rows
bind_rows(df1 %>% add_rownames(), 
          df2 %>% add_rownames()) %>% 
    # evaluate following calls for each value in the rowname column
    group_by(rowname) %>% 
    # add all non-grouping variables
    summarise_all(sum)

## # A tibble: 7 x 4
##   rowname     x     y     z
##     <chr> <int> <int> <int>
## 1       A     1     2     3
## 2       B     2     3     4
## 3       C     4     6     8
## 4       D     6     8    10
## 5       E     8    10    12
## 6       F     4     5     6
## 7       G     5     6     7
库(dplyr)
#在每个data.frame中将行名添加为列并绑定行
绑定_行(df1%>%add_rownames(),
df2%>%add_rownames())%>%
#对rowname列中的每个值计算以下调用
分组依据(行名称)%>%
#添加所有非分组变量
总结所有(总和)
###tibble:7 x 4
##行名称x y z
##        
##1A 12 3
##2B234
##3 C 4 6 8
##4D6810
##5 E 8 10 12
##6F456
##7G567

一个以R为基础的解决方案:

# create a new variable from the rownames
df1$rn <- rownames(df1)
df2$rn <- rownames(df2)

# bind the two dataframes together by row and aggregate
res <- aggregate(cbind(x,y,z) ~ rn, rbind(df1,df2), sum)
# or (thx to @alistaire for reminding me):
res <- aggregate(. ~ rn, rbind(df1,df2), sum)

# assign the rownames again
rownames(res) <- res$rn

# get rid of the 'rn' column
res <- res[, -1]

这可能需要进行一些测试,以使rownames逻辑在更长的示例中工作:

dfr <-rbind(df1,df2)
do.call(rbind, lapply( split(dfr, sapply(rownames(dfr),substr,1,1)), colSums))
  x  y  z
A 1  2  3
B 2  3  4
C 4  6  8
D 6  8 10
E 8 10 12
F 4  5  6
G 5  6  7

dfr另一种方法是将数据熔化并强制转换。首先,由于@Jaap,我们将行名称设置为两个数据帧的最后一列

df1$rn <- rownames(df1)
df2$rn <- rownames(df2)
然后我们使用dcast和mget函数,该函数用于一次检索多个变量

mydf<- dcast(melt(mget(ls(pattern = "df\\d+")), id.vars = "rn"), 
      rn ~ variable, value.var = "value", fun.aggregate = sum)

rownames(mydf) <- mydf$rn

# get rid of the 'rn' column
mydf <- mydf[, -1]

> mydf
#  x  y  z
#A 1  2  3
#B 2  3  4
#C 4  6  8
#D 6  8 10
#E 8 10 12
#F 4  5  6
#G 5  6  7

mydf还可以对将dfs转换为矩阵的操作进行矢量化:

result_df <- as.data.frame(as.matrix(df1) + as.matrix(df2))

result\u df如果行名称在数据集中表示为不同的属性,那会起作用吗?奇怪的是,我从未在公式上下文中使用过
cbind
对我来说比cbind(x,y,z)
更有意义(我有点希望它是
+
),但后者实际上可能非常有用…@alistaire
cbind
在您不想将聚合函数应用于所有非分组列时特别有用(尽管我不得不承认我忘记了
选项;-)
melt(list(df1, df2), id.vars = "rn")
mydf<- dcast(melt(mget(ls(pattern = "df\\d+")), id.vars = "rn"), 
      rn ~ variable, value.var = "value", fun.aggregate = sum)

rownames(mydf) <- mydf$rn

# get rid of the 'rn' column
mydf <- mydf[, -1]

> mydf
#  x  y  z
#A 1  2  3
#B 2  3  4
#C 4  6  8
#D 6  8 10
#E 8 10 12
#F 4  5  6
#G 5  6  7
result_df <- as.data.frame(as.matrix(df1) + as.matrix(df2))