5

我有一个命名向量的列表(见下文和最后的dput版本)如果向量不包含名称(在这种情况下为字符),我想“合并”在一起以创建一个矩阵并填充零。这似乎并不难,但我还没有找到一个可行的基础解决方案。我考虑过使用 match,但是当我确定有一种奇特的方式可以一起使用时,这似乎非常do.call耗时rbind

命名向量列表:

$greg

e i k l 
1 2 1 1 

$sam

! c e i t 
1 1 1 2 1 

$teacher

? c i k l 
1 1 1 1 1 

最终期望的输出

           !  ?  c  e  i  k  l  t
greg       0  0  0  1  2  1  1  0 
sam        1  0  1  1  2  0  0  1 
teacher    0  1  1  0  1  1  1  0 

可能这是人们会给出的输出,用 0 填充 NA 很容易

           !  ?  c  e  i  k  l  t
greg      NA NA NA  1  2  1  1 NA 
sam        1 NA  1  1  2 NA NA  1 
teacher   NA  1  1 NA  1  1  1 NA 

样本数据

L2 <- structure(list(greg = structure(c(1L, 2L, 1L, 1L), .Dim = 4L, .Dimnames = structure(list(
        c("e", "i", "k", "l")), .Names = ""), class = "table"), sam = structure(c(1L, 
    1L, 1L, 2L, 1L), .Dim = 5L, .Dimnames = structure(list(c("!", 
    "c", "e", "i", "t")), .Names = ""), class = "table"), teacher = structure(c(1L, 
    1L, 1L, 1L, 1L), .Dim = 5L, .Dimnames = structure(list(c("?", 
    "c", "i", "k", "l")), .Names = ""), class = "table")), .Names = c("greg", 
    "sam", "teacher"))
4

4 回答 4

6

这是一个相当简单的基本解决方案:

# first determine all possible column names
cols <- sort(unique(unlist(lapply(L2,names), use.names=FALSE)))
# initialize the output
out <- matrix(0, length(L2), length(cols), dimnames=list(names(L2),cols))
# loop over list and fill in the matrix
for(i in seq_along(L2)) {
  out[names(L2)[i], names(L2[[i]])] <- L2[[i]]
}

更新基准:

f1 <- function(L2) {
  cols <- sort(unique(unlist(lapply(L2,names), use.names=FALSE)))
  out <- matrix(0, length(L2), length(cols), dimnames=list(names(L2),cols))
  for(i in seq_along(L2)) out[names(L2)[i], names(L2[[i]])] <- L2[[i]]
  out
}   
f2 <- function(L2) {
  L.names <- sort(unique(unlist(sapply(L2, names))))
  L3 <- t(sapply(L2, function(x) x[L.names]))
  colnames(L3) <- L.names
  L3[is.na(L3)] <- 0
  L3
}
f3 <- function(L2) {
  m <- do.call(rbind, lapply(L2, as.data.frame))
  m$row <- sub("[.].*", "", rownames(m))
  m$Var1 <- factor(as.character(m$Var1))
  xtabs(Freq ~ row + Var1, m)
}
library(rbenchmark)
benchmark(f1(L2), f2(L2), f3(L2), order="relative")[,1:5]
#     test replications elapsed relative user.self
# 1 f1(L2)          100   0.022    1.000     0.020
# 2 f2(L2)          100   0.051    2.318     0.052
# 3 f3(L2)          100   0.788   35.818     0.760
set.seed(21)
L <- replicate(676, {n=sample(10,1); l=sample(26,n);
  setNames(sample(6,n,TRUE), letters[l])}, simplify=FALSE)
names(L) <- levels(interaction(letters,LETTERS))
benchmark(f1(L), f2(L), order="relative")[,1:5]
#    test replications elapsed relative user.self
# 1 f1(L)          100    1.84    1.000     1.828
# 2 f2(L)          100    4.24    2.304     4.220
于 2013-01-01T17:49:48.710 回答
4

我认为是这样的:

names <- sort(unique(unlist(lapply(L2, names), use.names=FALSE)))
L3 <- t(vapply(L2, function(x) x[names], FUN.VALUE=numeric(length(names))))
colnames(L3) <- names
L3[is.na(L3)] <- 0
于 2013-01-01T17:52:04.467 回答
3

reshape2 解决方案。这可以通过 reshape2 包轻松完成,方法是将列表熔化为长形式,然后使用dcast将其重新整形为宽形式:

> library(reshape2)
> m <- melt(L2)
> m$Var.1 <- factor(as.character(m$Var.1)) # optional - if columns should be sorted
> dcast(m, L1 ~ Var.1, fill = 0)
       L1 ! ? c e i k l t
1    greg 0 0 0 1 2 1 1 0
2     sam 1 0 1 1 2 0 0 1
3 teacher 0 1 1 0 1 1 1 0

基础溶液。这是一个相应的基本解决方案,其中前两行执行熔化,下一行确保对列进行排序,最后一行从长到宽重塑:

> m <- do.call(rbind, lapply(L2, as.data.frame))
> m$row <- sub("[.].*", "", rownames(m))
> m$Var1 <- factor(as.character(m$Var1))
> xtabs(Freq ~ row + Var1, m)
         Var1
row       ! ? c e i k l t
  greg    0 0 0 1 2 1 1 0
  sam     1 0 1 1 2 0 0 1
  teacher 0 1 1 0 1 1 1 0

编辑:添加了一个基本解决方案并修改了排序线。

于 2013-01-01T17:35:28.383 回答
1

在键入此内容时,我想到了此解决方案,但想知道是否有更有效的解决方案:

chars <- sort(unique(unlist(lapply(L2, names))))
L3 <- lapply(L2, function(x){
   nots <- chars[!chars %in% names(x)]
   new <- rev(c(x, rep(0, length(nots))))
   names(new)[1:length(nots)] <- nots
   new[order(names(new))]
})
do.call(rbind, L3)

产量:

        ! ? c e i k l t
greg    0 0 0 1 2 1 1 0
sam     1 0 1 1 2 0 0 1
teacher 0 1 1 0 1 1 1 0
于 2013-01-01T17:04:57.960 回答