如何在R中组合列表中的元素?

huangapple go评论49阅读模式
英文:

How do I combine elements from a list in R?

问题

以下是您提供的内容的翻译部分:

我在R中有一个名为`sims`的列表,其中包含404个元素。列表的每个元素本身都是一个具有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个字符。但`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`的每个列重复进行。

然后,我想将这种连接过程迭代到`sims[[5]]`到`sims[[8]]`和`sims[[9]]`到`sims[[12]]`等,一直到`sims[[404]]`为一组四个。我尝试了许

<details>
<summary>英文:</summary>

I have a list `sims` in R that contains 404 elements. Each element of the list is itself a list with a length of 24. Each of these lists has the same column names, namely `L1` to `L24`. 

So `sims[[1]]$L1` looks as follows:

$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"


For each internal list, the values are always character data and there are always the same number of character values. So `sims[[1]]$L1`...`sims[[1]]$L24` all have 55 characters as in the above example. But `sims[[4]]$L1`...`sims[[4]]$L24` has only one character value. 

I am trying to concatenate the internal lists. Specifically, I want to take every four internal lists of `sims` (i.e., sims[[1]] through sims[[4]], sims[[4]] through sims[[8]], sims[[8]] through sims[[12]], etc.) and concatenate them. 

`sims[[2]]$L1` looks as follows:

$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` looks as follows:

$L1
[1] "1" "0" "3" "1" "2" "2" "1" "2" "1" "2"


`sims[[4]]$L1` looks as follows:

$L1
"2"


So what I want is to concatenate `sims[[1]]` to `sims[[4]]` (each of which contains 24 columns) so that the `L1` have all of the above character values:

"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"

This concatenation process would occur for each of the 24 columns `L1` through `L24`.

I then want to iterate this contatenation process for `sims[[5]]` through `sims[[8]]` and `sims[[9]]` through `sims[[12]]`, all the way through to `sims[[404]]` in sets of four. I have tried many different things with `merge`, but have not even come close to a solution. How do I do this in R?

**EDIT**

Here is a simplified version of the list `sims`, which has 8 internal lists. Each of these internal lists has 3 columns, `L1`, `L2`, and `L3`. The values of each internal list are character data and the number of character values varies. 

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"


I want to concatenate the values of `sims[[1]]` through `sims[[4]]` and `sims[[5]]` through `sims[[8]]` to produce the following list:

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"



</details>


# 答案1
**得分**: 1

In Base R:

```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"))
英文:

In Base R:

f &lt;- rep(seq_along(sims), each=4, length=length(sims))
lapply(split(sims, f), \(x)do.call(Map, c(c, x))) 

$`1`
$`1`$L1
[1] &quot;0&quot; &quot;1&quot; &quot;0&quot; &quot;0&quot; &quot;1&quot; &quot;0&quot; &quot;0&quot; &quot;1&quot;

$`1`$L2
[1] &quot;1&quot; &quot;0&quot; &quot;0&quot; &quot;2&quot; &quot;0&quot; &quot;2&quot; &quot;1&quot; &quot;1&quot;

$`1`$L3
[1] &quot;1&quot; &quot;?&quot; &quot;0&quot; &quot;3&quot; &quot;3&quot; &quot;1&quot; &quot;3&quot; &quot;2&quot;


$`2`
$`2`$L1
[1] &quot;1&quot; &quot;1&quot; &quot;1&quot; &quot;0&quot; &quot;0&quot; &quot;0&quot; &quot;0&quot; &quot;0&quot;

$`2`$L2
[1] &quot;1&quot; &quot;2&quot; &quot;2&quot; &quot;1&quot; &quot;1&quot; &quot;2&quot; &quot;2&quot; &quot;1&quot;

$`2`$L3
[1] &quot;3&quot; &quot;1&quot; &quot;2&quot; &quot;3&quot; &quot;3&quot; &quot;0&quot; &quot;1&quot; &quot;1&quot;

sims &lt;- list(list(L1 = c(&quot;0&quot;, &quot;1&quot;, &quot;0&quot;), L2 = c(&quot;1&quot;, &quot;0&quot;, &quot;0&quot;), L3 = c(&quot;1&quot;, 
&quot;?&quot;, &quot;0&quot;)), list(L1 = c(&quot;0&quot;, &quot;1&quot;), L2 = c(&quot;2&quot;, &quot;0&quot;), L3 = c(&quot;3&quot;, 
&quot;3&quot;)), list(L1 = &quot;0&quot;, L2 = &quot;2&quot;, L3 = &quot;1&quot;), list(L1 = c(&quot;0&quot;, &quot;1&quot;
), L2 = c(&quot;1&quot;, &quot;1&quot;), L3 = c(&quot;3&quot;, &quot;2&quot;)), list(L1 = c(&quot;1&quot;, &quot;1&quot;, 
&quot;1&quot;), L2 = c(&quot;1&quot;, &quot;2&quot;, &quot;2&quot;), L3 = c(&quot;3&quot;, &quot;1&quot;, &quot;2&quot;)), list(L1 = &quot;0&quot;, 
    L2 = &quot;1&quot;, L3 = &quot;3&quot;), list(L1 = c(&quot;0&quot;, &quot;0&quot;, &quot;0&quot;), L2 = c(&quot;1&quot;, 
&quot;2&quot;, &quot;2&quot;), L3 = c(&quot;3&quot;, &quot;0&quot;, &quot;1&quot;)), list(L1 = &quot;0&quot;, L2 = &quot;1&quot;, L3 = &quot;1&quot;))

huangapple
  • 本文由 发表于 2023年3月31日 19:35:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定