R 列表到矩阵的复杂重排
很抱歉标题含糊不清。而且,一个例子胜过千言万语 我有一份清单:R 列表到矩阵的复杂重排,r,list,matrix,R,List,Matrix,很抱歉标题含糊不清。而且,一个例子胜过千言万语 我有一份清单: > lst<-list(A=c("one","two", "three"), B=c("two", "four", "five"), C=c("six", "seven"), D=c("one", "five", "eight")) > lst $A [1] "one" "two" "three" $B [1] "two" "four" "five" $C [1] "six" "seven"
> lst<-list(A=c("one","two", "three"), B=c("two", "four", "five"), C=c("six", "seven"), D=c("one", "five", "eight"))
> lst
$A
[1] "one" "two" "three"
$B
[1] "two" "four" "five"
$C
[1] "six" "seven"
$D
[1] "one" "five" "eight"
其中,基本上,每个坐标表示每个列表元素中每个列表值的存在(1)或不存在(0)
我尝试过处理as.data.frame()、unlist()、table()和melt()的各种组合,但没有成功,因此任何指向正确方向的指针都将非常有用
我想我最后的办法是使用嵌套循环,循环遍历列表元素,然后将0或1分配给矩阵中相应的坐标,但这似乎过于复杂
for (...) {
for (...) {
if (...) {
var <- 1
} else {
var <- 0
}
}
}
(…){
对于(…){
如果(…){
var这里有一个相当手动的方法:
t(table(rep(names(lst), sapply(lst, length)), unlist(lst)))
#
# A B C D
# eight 0 0 0 1
# five 0 1 0 1
# four 0 1 0 0
# one 1 0 0 1
# seven 0 0 1 0
# six 0 0 1 0
# three 1 0 0 0
# two 1 1 0 0
而且,
stack
也可以工作
table(stack(lst))
# ind
# values A B C D
# eight 0 0 0 1
# five 0 1 0 1
# four 0 1 0 0
# one 1 0 0 1
# seven 0 0 1 0
# six 0 0 1 0
# three 1 0 0 0
# two 1 1 0 0
更新1 如果您关心行和列的顺序,您可以在使用
表之前显式地考虑它们:
A <- stack(lst)
A$values <- factor(A$values,
levels=c("one", "two", "three", "four",
"five", "six", "seven", "eight"))
A$ind <- factor(A$ind, c("A", "B", "C", "D"))
table(A)
您正在寻找函数的正确位置。也许table
就是您实际要寻找的函数…感谢您富有洞察力的回答!仅供参考,我接受了另一个答案,因为table(melt())方法对我来说是最简单、最容易记住的方法。@rent0n,没问题。众所周知,“reforme2”(最近,“data.table”)获得所有的爱……)
table(stack(lst))
# ind
# values A B C D
# eight 0 0 0 1
# five 0 1 0 1
# four 0 1 0 0
# one 1 0 0 1
# seven 0 0 1 0
# six 0 0 1 0
# three 1 0 0 0
# two 1 1 0 0
A <- stack(lst)
A$values <- factor(A$values,
levels=c("one", "two", "three", "four",
"five", "six", "seven", "eight"))
A$ind <- factor(A$ind, c("A", "B", "C", "D"))
table(A)
set.seed(1)
vec <- sample(3:10, 50, replace = TRUE)
lst <- lapply(vec, function(x) sample(letters, x))
names(lst) <- paste("A", sprintf("%02d", sequence(length(lst))), sep = "")
library(reshape2)
library(microbenchmark)
R2 <- function() table(melt(lst))
S <- function() table(stack(lst))
U <- function() t(table(rep(names(lst), sapply(lst, length)), unlist(lst, use.names=FALSE)))
microbenchmark(R2(), S(), U())
# Unit: microseconds
# expr min lq median uq max neval
# R2() 36836.579 37521.295 38053.9710 40213.829 45199.749 100
# S() 1427.830 1473.210 1531.9700 1565.345 3776.860 100
# U() 892.265 906.488 930.5575 945.326 1261.592 100