输入文件:
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对的成对值,如下所示:
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列的新数据框(两个行名,后跟相乘的值)。
是
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和@Owen的答案元素,以提供一种真正的矢量化方法来解决此问题。 (您可以将组合名称添加为行名称,最后增加一个步骤。)
首先,数据:
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
的行索引:
combs <- combn(seq_len(nrow(dat)), 2)
combs
的行现在索引了我们要相乘的dat
的行:
> 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, ]
,其中i
和j
分别是combs
的第一行和第二行。我们将其转换为带有data.matrix()
的矩阵,因为这对于大型对象将更为有效,但是代码也可以将dat
作为数据帧使用。
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,], ]
产生一个矩阵,其中各行按组合指定的顺序重复:
> 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()
调用来修改行名:
> 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
dat <- read.table(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)
> combos <- combn(rn,2)
> combos
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] "w" "w" "w" "x" "x" "y"
[2,] "x" "y" "z" "y" "z" "z"
apply(combos,2, function(x) c(x[1], x[2], unlist(dat[x[1],]*dat[x[2],])))
[,1] [,2] [,3] [,4] [,5] [,6]
"w" "w" "w" "x" "x" "y"
"x" "y" "z" "y" "z" "z"
A "0" "0" "0" "0" "0" "0"
B "0" "0" "0" "0" "0" "0"
C "0" "1" "0" "0" "0" "0"
D "1" "1" "1" "1" "1" "1"
所以是最终解决方案:
t( apply(combos,2, function(x) c(x[1], x[2], unlist(dat[x[1],]*dat[x[2],]))) )
如果将组合转换为数据框,您还可以将bindmatrix转换为“数字”类型:
cbind( as.data.frame(t(combos)),
t( apply(combos,2, function(x)
unlist(dat[x[1],]*dat[x[2],]))) )
V1 V2 A B C D
1 w x 0 0 0 1
2 w y 0 0 1 1
3 w z 0 0 0 1
4 x y 0 0 0 1
5 x z 0 0 0 1
6 y z 0 0 0 1
如果要乘以行,建议转换为矩阵:
> m = as.matrix(df1)
> m["x", ] * m["y", ]
A B C D
0 0 0 1
您希望通过plyr
获得的特定结果,
library(plyr)
ldply(1:(nrow(m)-1), function(i)
ldply((i+1):nrow(m), function(j) {
a = row.names(m)[[i]]
b = row.names(m)[[j]]
do.call(data.frame,
c(list(a=a, b=b), m[i,] * m[j,])
)
})
)
抱歉的是,它看起来有些不可思议-data.frames并不是真的像“行一样”。线
do.call(data.frame,
c(list(a=a, b=b), m[i,] * m[j,])
)
在6列中传递:名称的a和b,并与(乘以c
)到相乘的行。
使用惊人的plyr包的更短方法(我认为)
您的数据框
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))
YOUR_COMBS<-combn(rownames(df1),2)
以及您的结果:)
require(plyr) #(version 1.81...in version 1.82 you can take the annoying 'X1' index out... )
YOUR_RESULTS<-adply(YOUR_COMBS,2,function(x) {
tmp_row<-data.frame(Comb=paste0(x,collapse = ''),df1[x[1],]*df1[x[2],])
})