如何在列名称相同的情况下添加数据框 [英] how to add dataframes where the column names are same

查看:44
本文介绍了如何在列名称相同的情况下添加数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据框 df1 df2 。我想创建一个新的数据框 df3 ,它只是 df1 df2的总和列名相同。

I have two data frame df1 and df2. I want to create a new data frame df3 which is simply the sum ofdf1 and df2 where the column names are same.

df1 <- data.frame(x1=c(1,4,5),x2=c(5,6,7),x3=c(9,9,10))
df2 <- data.frame(x1=c(1,6,3),x2=c(4,3,1),x3=c(5,4,6),x4=c(7,6,7))

 df1
    x1  x2  x3
 1  1   5   9
 2  4   6   9
 3  5   7   10

df2
    x1  x2  x3  x4
 1  1   4   5   7
 2  6   3   4   6
 3  3   1   6   7

df3
    x1  x2  x3  x4
 1  2   9   14  7
 2  10  9   13  6
 3  8   8   16  7


推荐答案

我们找到在'df1'和'df2'('nm1')中都通用的列名。创建 df2( df3)的副本。添加数据集的子集( df1 [nm1] df2 [nm1] )并将其分配给的相应子集'df3'。

We find the column names that are common in both 'df1' and 'df2' ('nm1'). Create a copy of 'df2' ('df3'). Add the subset of datasets (df1[nm1], df2[nm1]) and assign it to the corresponding subset of 'df3'.

nm1 <- intersect(names(df1), names(df2))
df3 <- df2
df3[nm1] <- df1[nm1]+df2[nm1]
df3
#  x1 x2 x3 x4
#1  2  9 14  7
#2 10  9 13  6
#3  8  8 16  7






如果'df1'中还有其他唯一列不在'df2'中,反之亦然,一种选择是将数据集放置在列表,然后使用 rbindlist rbind (来自 data.table ),创建一个序列列('N')并使用 lapply 来获取每个列的 sum


In case there are other unique columns in 'df1' that are not in 'df2' and viceversa, one option would be to place the datasets in a list, then rbind them with rbindlist (from data.table), create a sequence column ('N') and use lapply to get the sum of each of the columns.

library(data.table)
rbindlist(list(df1, df2), fill=TRUE, idcol=TRUE)[,
       N:= 1:.N, .id][,lapply(.SD, sum, na.rm=TRUE) , 
           by = N , .SDcols=x1:x4][, N:= NULL][]
#   x1 x2 x3 x4
#1:  2  9 14  7
#2: 10  9 13  6
#3:  8  8 16  7

这篇关于如何在列名称相同的情况下添加数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆