- Работает с
data.frame
data.frame(lapply(X = split.default(x = cbind(df1, df2),
f = c(names(df1), names(df2))),
FUN = rowSums))[names(df1)]
# a b c
#1 1 0 1
#2 0 0 1
Работает с
data.frame
и
matrix
nm = intersect(colnames(df1), colnames(df2))
nm1 = colnames(df1)[!colnames(df1) %in% nm]
m = cbind(df1[, nm1, drop = FALSE], df1[, nm, drop = FALSE] + df2[, nm, drop = FALSE])
colnames(m) = c(nm1, nm)
m[,colnames(df1)]
# a b c
#1 1 0 1
#2 0 0 1
#DATA
df1 = structure(list(a = c(0L, 0L), b = c(0L, 0L), c = c(0L, 0L)),
class = "data.frame",
row.names = c(NA, -2L))
df2 = structure(list(a = 1:0, c = c(1L, 1L), d = c(0L, 0L)),
class = "data.frame",
row.names = c(NA, -2L))