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

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

How do I combine elements from a list in R?

问题

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

  1. 我在R中有一个名为`sims`的列表,其中包含404个元素。列表的每个元素本身都是一个具有24个元素的列表。这些列表的每个元素都具有相同的列名,即`L1``L24`
  2. 因此,`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"

  1. 对于每个内部列表,值始终为字符数据,并且始终具有相同数量的字符值。因此,`sims[[1]]$L1`...`sims[[1]]$L24`都具有与上面示例中相同的55个字符。但`L1`...`sims[[4]]$L24`仅具有一个字符值。
  2. 我试图连接内部列表。具体来说,我想取`sims`的每四个内部列表(即`sims[[1]]``sims[[4]]``sims[[4]]``sims[[8]]``sims[[8]]``sims[[12]]`等)并连接它们。
  3. `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"

  1. `sims[[3]]$L1`如下所示:

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

  1. `sims[[4]]$L1`如下所示:

$L1
"2"

  1. 因此,我想连接`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"

  1. 这种连接过程将针对`L1``L24`的每个列重复进行。
  2. 然后,我想将这种连接过程迭代到`sims[[5]]``sims[[8]]``sims[[9]]``sims[[12]]`等,一直到`sims[[404]]`为一组四个。我尝试了许
  3. <details>
  4. <summary>英文:</summary>
  5. 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`.
  6. 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"

  1. 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.
  2. 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.
  3. `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"

  1. `sims[[3]]$L1` looks as follows:

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

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

$L1
"2"

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

  1. This concatenation process would occur for each of the 24 columns `L1` through `L24`.
  2. 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?
  3. **EDIT**
  4. 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"

  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"

  1. </details>
  2. # 答案1
  3. **得分**: 1
  4. In Base R:
  5. ```R
  6. f <- rep(seq_along(sims), each=4, length=length(sims))
  7. lapply(split(sims, f), \(x)do.call(Map, c(c, x)))
  8. $`1`
  9. $`1`$L1
  10. [1] "0" "1" "0" "0" "1" "0" "0" "1"
  11. $`1`$L2
  12. [1] "1" "0" "0" "2" "0" "2" "1" "1"
  13. $`1`$L3
  14. [1] "1" "?" "0" "3" "3" "1" "3" "2"
  15. $`2`
  16. $`2`$L1
  17. [1] "1" "1" "1" "0" "0" "0" "0" "0"
  18. $`2`$L2
  19. [1] "1" "2" "2" "1" "1" "2" "2" "1"
  20. $`2`$L3
  21. [1] "3" "1" "2" "3" "3" "0" "1" "1"
  1. sims <- list(list(L1 = c("0", "1", "0"), L2 = c("1", "0", "0"), L3 = c("1", "?", "0")),
  2. list(L1 = c("0", "1"), L2 = c("2", "0"), L3 = c("3", "3")),
  3. list(L1 = "0", L2 = "2", L3 = "1"),
  4. list(L1 = c("0", "1"), L2 = c("1", "1"), L3 = c("3", "2")),
  5. list(L1 = c("1", "1", "1"), L2 = c("1", "2", "2"), L3 = c("3", "1", "2")),
  6. list(L1 = "0", L2 = "1", L3 = "3"),
  7. list(L1 = c("0", "0", "0"), L2 = c("1", "2", "2"), L3 = c("3", "0", "1")),
  8. list(L1 = "0", L2 = "1", L3 = "1"))
英文:

In Base R:

  1. f &lt;- rep(seq_along(sims), each=4, length=length(sims))
  2. lapply(split(sims, f), \(x)do.call(Map, c(c, x)))
  3. $`1`
  4. $`1`$L1
  5. [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;
  6. $`1`$L2
  7. [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;
  8. $`1`$L3
  9. [1] &quot;1&quot; &quot;?&quot; &quot;0&quot; &quot;3&quot; &quot;3&quot; &quot;1&quot; &quot;3&quot; &quot;2&quot;
  10. $`2`
  11. $`2`$L1
  12. [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;
  13. $`2`$L2
  14. [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;
  15. $`2`$L3
  16. [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;

  1. 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;,
  2. &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;,
  3. &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;
  4. ), 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;,
  5. &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;,
  6. 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;,
  7. &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:

确定