逐行乘以数据框 [英] Multiply a data frame row-by-row

查看:58
本文介绍了逐行乘以数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入文件:

df1 <- data.frame(row.names=c("w","x","y","z"), 
                  A=c(0,0,0,0),
                  B=c(0,1,0,0), 
                  C=c(1,0,1,0), 
                  D=c(1,1,1,1))

  A B C D
w 0 0 1 1
x 0 1 0 1
y 0 0 1 1
z 0 0 0 1

我想应用一个方程,即行 w 乘以行 x 以获得 w-x 对的成对值,如下所示:

I want to apply an equation i.e. multiply row w to row x to get the pairwise value for w-x pair, as follows:

      A B C D
    w 0 0 1 1
X   x 0 1 0 1
--------------
   wx 0 0 0 1

对 w-x、w-y、w-y、w-z、x-y、x-z、y-z 进行逐行分析.并生成一个具有 6 列的新数据框(两个行名称,后跟相乘值).

to get row-wise analysis for w-x, w-y, w-y, w-z, x-y, x-z, y-z. and generate a new dataframe with 6 columns (two row names followed by the multiplied values).

就是这样

w x 0 0 0 1
w y 0 0 1 1
w z 0 0 0 1
x y 0 0 0 1
x z 0 0 0 1
y z 0 0 0 1

谢谢.

推荐答案

如果您不想在结果对象中使用组合名称,那么我们可以结合@DWin's 和@Owen's Answers 的元素来提供真正的矢量化方法问题.(您可以将组合名称添加为行名称,并在末尾添加一个额外步骤.)

If you don't want the combo names in the resulting object, then we can combine elements of @DWin's and @Owen's Answers to provide a truly vectorised approach to the problem. (You can add the combination names as row names with one extra step at the end.)

一、数据:

dat <- read.table(con <- textConnection("  A B C D
w 0 0 1 1
x 0 1 0 1
y 0 0 1 1
z 0 0 0 1
"), header=TRUE)
close(con)

从@DWin 的答案中获取 combn() 的想法,但在 dat行索引上使用它:

Take the combn() idea from @DWin's Answer but use it on the row indices of dat:

combs <- combn(seq_len(nrow(dat)), 2)

combs 的行现在索引我们想要相乘的 dat 的行:

The rows of combs now index the rows of dat that we want to multiply together:

> combs
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    1    1    2    2    3
[2,]    2    3    4    3    4    4

现在我们采用@Owen 展示的想法,即 dat[i, ] * dat[j, ]ijcombs 的第一行和第二行.我们使用 data.matrix() 转换为矩阵,因为这对于大型对象会更有效,但代码也将使用 dat 作为数据框.>

Now we take the idea @Owen showed, namely dat[i, ] * dat[j, ] with i and j being the first and second rows of combs respectively. We convert to a matrix with data.matrix() as this will be more efficient for large objects, but the code will work with dat as a data frame too.

mat <- data.matrix(dat)
mat[combs[1,], ] * mat[combs[2,], ]

产生:

> mat[combs[1,], ] * mat[combs[2,], ]
  A B C D
w 0 0 0 1
w 0 0 1 1
w 0 0 0 1
x 0 0 0 1
x 0 0 0 1
y 0 0 0 1

要了解其工作原理,请注意 mat[combs[k,], ] 生成一个矩阵,其中各行按组合指定的顺序重复:

To see how this works, note that mat[combs[k,], ] produces a matrix with various rows repeated in the order specified by the combinations:

> mat[combs[1,], ]
  A B C D
w 0 0 1 1
w 0 0 1 1
w 0 0 1 1
x 0 1 0 1
x 0 1 0 1
y 0 0 1 1
> mat[combs[2,], ]
  A B C D
x 0 1 0 1
y 0 0 1 1
z 0 0 0 1
y 0 0 1 1
z 0 0 0 1
z 0 0 0 1

为了准确获取 OP 发布的内容,我们可以使用第二个 combn() 调用修改行名:

To get exactly what the OP posted, we can modify the rownames using a second combn() call:

> out <- mat[combs[1,], ] * mat[combs[2,], ]
> rownames(out) <- apply(combn(rownames(dat), 2), 2, paste, collapse = "")
> out
   A B C D
wx 0 0 0 1
wy 0 0 1 1
wz 0 0 0 1
xy 0 0 0 1
xz 0 0 0 1
yz 0 0 0 1

这篇关于逐行乘以数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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