我正在尝试将因素列表转换为矩阵,例如:
我的列表:
[[1]]
[1] "RA" "FZFG" "BR"
[[2]]
[1] "RA"
[[3]]
[1] ""
[[4]]
[1] ""
到
RA FZFG BR
1 1 1
1 0 0
0 0 0
0 0 0
我尝试执行以下操作:
allFactors<-c("RA","FZFG","BR")
mat<-model.matrix(~allFactors, data =myLists)
但是有这个错误:
Error in data.frame(c("RA", "FZFG", "BR"), "RA", "", "", "", "", c("RA", : arguments imply differing number of rows: 3, 1, 2, 4, 5, 7, 6, 8, 9
感谢任何帮助。
最佳答案
一个选项是
library(qdapTools)
mtabulate(myLists)[-1]
或者使用base R
table(stack(setNames(myLists, seq_along(myLists)))[2:1])[,-1]
关于r - r 中列表中的 model.matrix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30427235/