我有一个包含404个元素的R列表sims
。列表中的每个元素本身是一个长度为24的列表。每个列表都有相同的列名,即L1
到L24
。
因此sims[[1]]$L1
看起来如下:
$L1
[1] "1" "1" "0" "0" "1" "1" "0" "0" "1" "0" "0" "1" "0" "1" "0" "0" "0" "1" "0" "1" "1"
[22] "0" "0" "0" "0" "1" "1" "0" "0" "0" "0" "0" "1" "0" "0" "0" "0" "1" "0" "0" "0" "0"
[43] "1" "0" "1" "1" "0" "1" "0" "0" "1" "0" "1" "1" "0"
对于每个内部列表,值总是字符数据,并且总是有相同数量的字符值。所以sims[[1]]$L1
…sims[[1]]$L24
和上面的例子一样都有55个字符。但是sims[[4]]$L1
……sims[[4]]$L24
只有一个字符值
我正在尝试连接内部列表。具体来说,我想取sims
的每四个内部列表(即sims[[1]]到sims[[4]], sims[[4]]到sims[[8]], sims[[8]]到sims[[12]],等等)并将它们连接起来。
sims[[2]]$L1
如下图所示:
$L1
[1] "1" "0" "0" "0" "0" "0" "1" "2" "2" "0" "2" "0" "1" "1" "2" "1" "0" "0" "0" "1"
sims[[3]]$L1
如下图所示:
$L1
[1] "1" "0" "3" "1" "2" "2" "1" "2" "1" "2"
sims[[4]]$L1
如下图所示:
$L1
"2"
所以我想要的是将sims[[1]]
连接到sims[[4]]
(每个包含24列),以便L1
具有上述所有字符值:
"1" "1" "0" "0" "1" "1" "0" "0" "1" "0" "0" "1" "0" "1" "0" "0" "0" "1" "0" "1" "1"
"0" "0" "0" "0" "1" "1" "0" "0" "0" "0" "0" "1" "0" "0" "0" "0" "1" "0" "0" "0" "0"
"1" "0" "1" "1" "0" "1" "0" "0" "1" "0" "1" "1" "0" "1" "0" "0" "0" "0" "0" "1" "2" "2" "0" "2" "0" "1" "1" "2" "1" "0" "0" "0" "1" "1" "0" "3" "1" "2" "2" "1" "2" "1" "2" "2"
这个连接过程将对L1
到L24
这24列中的每一列进行。
然后我想迭代sims[[5]]
到sims[[8]]
和sims[[9]]
到sims[[12]]
的这个连接过程,一直到sims[[404]]
,每组4个。我用merge
尝试了许多不同的东西,但甚至没有接近解决方案。在R里怎么做呢?
编辑
下面是列表sims
的简化版本,它有8个内部列表。每个内部列表都有3列,L1
、L2
和L3
。每个内部列表的值都是字符数据,字符值的个数各不相同。
sims[[1]]$L1 "0" "1" "0"
sims[[1]]$L2 "1" "0" "0"
sims[[1]]$L3 "1" "?" "0"
sims[[2]]$L1 "0" "1"
sims[[2]]$L2 "2" "0"
sims[[2]]$L3 "3" "3"
sims[[3]]$L1 "0"
sims[[3]]$L2 "2"
sims[[3]]$L3 "1"
sims[[4]]$L1 "0" "1"
sims[[4]]$L2 "1" "1"
sims[[4]]$L3 "3" "2"
sims[[5]]$L1 "1" "1" "1"
sims[[5]]$L2 "1" "2" "2"
sims[[5]]$L3 "3" "1" "2"
sims[[6]]$L1 "0"
sims[[6]]$L2 "1"
sims[[6]]$L3 "3"
sims[[7]]$L1 "0" "0" "0"
sims[[7]]$L2 "1" "2" "2"
sims[[7]]$L3 "3" "0" "1"
sims[[8]]$L1 "0"
sims[[8]]$L2 "1"
sims[[8]]$L3 "1"
我想将sims[[1]]
到sims[[4]]
和sims[[5]]
到sims[[8]]
的值连接起来,产生以下列表:
sims[[1]]$L1 "0" "1" "0" "0" "1" "0" "0" "1"
sims[[1]]$L2 "1" "0" "0" "2" "0" "2" "1" "1"
sims[[1]]$L3 "1" "?" "0" "3" "3" "1" "3" "2"
sims[[2]]$L1 "1" "1" "1" "0" "0" "0" "0" "0"
sims[[2]]$L2 "1" "2" "2" "1" "1" "2" "2" "1"
sims[[2]]$L3 "3" "1" "2" "3" "3" "0" "1" "1"
In Base R:
f <- rep(seq_along(sims), each=4, length=length(sims))
lapply(split(sims, f), (x)do.call(Map, c(c, x)))
$`1`
$`1`$L1
[1] "0" "1" "0" "0" "1" "0" "0" "1"
$`1`$L2
[1] "1" "0" "0" "2" "0" "2" "1" "1"
$`1`$L3
[1] "1" "?" "0" "3" "3" "1" "3" "2"
$`2`
$`2`$L1
[1] "1" "1" "1" "0" "0" "0" "0" "0"
$`2`$L2
[1] "1" "2" "2" "1" "1" "2" "2" "1"
$`2`$L3
[1] "3" "1" "2" "3" "3" "0" "1" "1"
sims <- list(list(L1 = c("0", "1", "0"), L2 = c("1", "0", "0"), L3 = c("1",
"?", "0")), list(L1 = c("0", "1"), L2 = c("2", "0"), L3 = c("3",
"3")), list(L1 = "0", L2 = "2", L3 = "1"), list(L1 = c("0", "1"
), L2 = c("1", "1"), L3 = c("3", "2")), list(L1 = c("1", "1",
"1"), L2 = c("1", "2", "2"), L3 = c("3", "1", "2")), list(L1 = "0",
L2 = "1", L3 = "3"), list(L1 = c("0", "0", "0"), L2 = c("1",
"2", "2"), L3 = c("3", "0", "1")), list(L1 = "0", L2 = "1", L3 = "1"))