且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在列名称相同的情况下添加数据框

更新时间:2023-09-20 19:04:46

我们找到在'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'中,反之亦然,一种选择是将数据集放置在列表$ c $中c>,然后使用 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