通过匹配列名称上每行的值来填充列值

时间:2012-07-27 19:45:43

标签: r

我想创建一个这样的数据框:

Label   Jim Charles Kevin Alan
Charles 0   1       0     0  
Kevin   0   0       1     0
Alan    0   0       0     1
Alan    0   0       0     1
Jim     1   0       0     0

我从一个数据框开始,其中列名已设置,人名在第一列中列出,但所有数字都是0.我希望能够通过匹配来快速将其中一些设置为1列名称的第一列中列出的名称。

2 个答案:

答案 0 :(得分:3)

可能有更快的方法,但这应该运作得相当好:

数据:

m <- matrix(0,nrow=5,ncol=4,
            dimnames=list(c("Charles","Kevin","Alan","Alan","Jim"),
            c("Jim","Charles","Kevin","Alan")))

使用outer比较所有列的所有行:

mm <- outer(rownames(m),colnames(m),"==")
storage.mode(mm) <- "numeric" ## because as.numeric() loses matrix dimensions
dimnames(mm) <- dimnames(m)   ## reset row/column names

答案 1 :(得分:2)

与Ben的答案类似,如果您的数据位于名为df的data.frame中:

df <- structure(list(Label = c("Charles", "Kevin", "Alan", "Alan", 
"Jim"), Jim = c(0, 0, 0, 0, 0), Charles = c(0, 0, 0, 0, 0), Kevin = c(0, 
0, 0, 0, 0), Alan = c(0, 0, 0, 0, 0)), .Names = c("Label", "Jim", 
"Charles", "Kevin", "Alan"), row.names = c(NA, -5L), class = "data.frame")

df[outer(df$Label, names(df), '==')] <- 1