Input file:
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,
If you want to multiply rows, I recommend converting to a matrix:
> m = as.matrix(df1)
> m["x", ] * m["y", ]
A B C D
0 0 0 1
The specific result you want you could get with 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,])
)
})
)
Sorry part of that looks a little magical -- data.frames aren't really meant to be "row like". The lines
do.call(data.frame,
c(list(a=a, b=b), m[i,] * m[j,])
)
pass in the 6 columns: a and b for the names, concatenated (with c) to the multiplied row.