R将多个列联表合并为一个列联表

R将多个列联表合并为一个列联表,r,lapply,sapply,tapply,R,Lapply,Sapply,Tapply,我有以下列示表 structure(list(`1` = structure(c(1L, 1L, 1L, 0L, 1L, 0L), .Dim = 2:3, .Dimnames = structure(list( c("a", "b"), c("x", "y", "z")), .Names = c("", "")), class = "table"), `2` = structure(c(1L, 1L, 0L, 1L), .Dim = c(2L, 2L), .Dimnames = structu

我有以下列示表

structure(list(`1` = structure(c(1L, 1L, 1L, 0L, 1L, 0L), .Dim = 2:3, .Dimnames = structure(list(
c("a", "b"), c("x", "y", "z")), .Names = c("", "")), class = "table"), 
`2` = structure(c(1L, 1L, 0L, 1L), .Dim = c(2L, 2L), .Dimnames = structure(list(
    c("b", "c"), c("y", "z")), .Names = c("", "")), class = "table")), .Names = c("1", "2"))
这个列表有两个列联表,我想把它们合并成一个。我尝试了这个解决方案,但没有成功。它给出了以下错误

> tapply(T,names(T),sum)
Error in FUN(X[[1L]], ...) : invalid 'type' (list) of argument
预期产量为

> table(x[[1]],x[[2]])

   x y z
 a 1 1 1
 b 1 1 0
 c 0 1 1
其中
x

structure(list(id = c("a", "a", "a", "b", "b", "c", "c"), upc = c("x","y", "z", "x", "y", "z", "y"), sfactor = c("1", "1", "1", "1","2", "2", "2")), .Names = c("id", "upc", "sfactor"), row.names = c(NA, -7L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x25aa378>)
结构(列表(id=c(“a”、“a”、“a”、“b”、“b”、“c”、“c”)、upc=c(“x”、“y”、“z”、“x”、“y”、“z”、“z”、“y”)、sfactor=c(“1”、“1”、“1”、“2”、“2”、“2”)、.Names=c(“id”、“upc”、“sfactor”)、row.Names=c(NA、-7L)、class=c(“数据表”、“数据框架”)、.internal.selfref=)

谢谢你的帮助。提前感谢。

将表格转换为长
数据。帧
,然后收集计数:

xtabs(Freq ~ ., data=do.call(rbind, lapply(L, data.frame) ))

#    Var2
#Var1 x y z
#   a 1 1 1
#   b 1 1 0
#   c 0 1 1

我们还可以使用
dcast
fun.aggregate
作为
sum
rbind
之后使用
rbindlist
列表
元素进行排序

library(data.table)
dcast(rbindlist(lapply(L, as.data.frame)),
              Var1~Var2, value.var="Freq", sum)
#   Var1 x y z
#1:    a 1 1 1
#2:    b 1 1 0
#3:    c 0 1 1
如果不需要“Var1”列,可以使用
acast
(来自
restrape2

library(reshape2)
acast(rbindlist(lapply(L, as.data.frame)), 
             Var1~Var2, value.var="Freq", sum)
#   x y z
# a 1 1 1
# b 1 1 0
# c 0 1 1