有没有一种很好的方法对两个数据帧的所有列配对进行操作?

时间:2011-06-21 14:03:29

标签: r

例如,给定数据框:

> df1
  a b
1 1 3
2 2 4

> df2
   x  y  z
1 10 12 14
2 11 13 15

并对df1和df2的每一对列进行加法运算,我想生成:

> df3
  ax bx ay by az bz
1 11 13 13 15 15 17
2 13 15 15 17 17 19

我编写了以下代码来完成这项工作,但我想知道是否有更好的方法可以做到这一点。

df1 <- data.frame(a=1:2, b=3:4)
df2 <- data.frame(x=10:11, y=12:13, z=14:15)

byColumnAdditionAllPairs <- function(df1, df2) {

    doOp <- function(x, df1, df2, pairs) { 
        i <- pairs[x,1]; # ith column of df1
        j <- pairs[x,2]; # jth column of df2

        # add paired columns
        tmp <- df1[i] + df2[j];

        # set new column name
        names(tmp)[1] <- paste(names(df1)[i], names(df2)[j], sep="");

        # return column
        tmp
    }

    # generate column pairings
    pairs <- expand.grid(1:length(df1), 1:length(df2))

    # for each column pair, doOp
    data.frame(sapply(1:nrow(pairs), doOp, df1, df2, pairs))
}

df3 <- byColumnAdditionAllPairs(df1, df2)

谢谢, 扎克

4 个答案:

答案 0 :(得分:5)

这是一种方式。它有一些其他答案的元素......

z <- outer(colnames(df1), colnames(df2), function(c1,c2) df1[,c1] + df2[,c2])
colnames(z) <- outer(colnames(df1), colnames(df2), paste, sep = '')     

> z
  ax bx ay by az bz
1 11 13 13 15 15 17
2 13 15 15 17 17 19

答案 1 :(得分:3)

使用expand.grid获取列对的所有名称。

col_pairs <- expand.grid(colnames(df1), colnames(df2))

现在应用你的添加功能

col_sums <- apply(col_pairs, 1L, function(x) df1[, x["Var1"]] + df2[, x["Var2"]])

修正列名

col_names <- apply(col_pairs, 1L, function(x) paste(x, collapse = ""))
colnames(col_sums) <- col_names

答案 2 :(得分:3)

comb <- as.vector(outer(names(df1),names(df2),paste))
df3 <- data.frame(sapply(comb,function(x) df1[strsplit(x," ")[[1]][1]]+df2[strsplit(x," ")[[1]][2]]))  
names(df3) <- gsub(" ","",comb)

给出了:

> df3
  ax bx ay by az bz
1 11 13 13 15 15 17
2 13 15 15 17 17 19

答案 3 :(得分:2)

稍微不同的方法是使用outer()

df1 <- data.frame(a = 1:2, b = 3:4)
df2 <- data.frame(x = 10:11, y = 12:13, z = 14:15)
m1 <- data.matrix(df1)
m2 <- data.matrix(df2)
t(sapply(1:2, function(x, m1, m2) outer(m1[x,], m2[x,], "+"), m1 = m1, m2 = m2))

给出:

> t(sapply(1:2, function(x, m1, m2) outer(m1[x,], m2[x,], "+"), m1 = m1, m2 = m2))
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]   11   13   13   15   15   17
[2,]   13   15   15   17   17   19