简体   繁体   English

在R中的第n个向量中分割向量列表

[英]Split list of vectors in nth vectors in R

I have a list in R with three vectors inside, each of them having 32 numeric elements inside. 我在R中有一个列表,里面有三个向量,每个向量里面都有32个数字元素。

I would like to split this list, so that I can get 32 vectors of length 3, each one corresponding to the three first elements of the three vectors in the list, the three second elements of the three vectors and so on. 我想拆分此列表,以便获得32个长度为3的向量,每个向量对应于列表中三个向量的三个第一元素,三个向量的三个第二元素,依此类推。

Say, my list has this aspect: 说,我的清单有以下方面:

$`1`
 [1] 0.00000000 0.00000000 0.00000000 0.01538462 0.01538462 0.01538462 0.01538462 0.13846154 0.00000000 0.00000000
[11] 0.00000000 0.03076923 0.00000000 0.01538462 0.04615385 0.20000000 0.00000000 0.01538462 0.00000000 0.04615385
[21] 0.00000000 0.00000000 0.01538462 0.15384615 0.00000000 0.01538462 0.00000000 0.01538462 0.01538462 0.01538462
[31] 0.03076923 0.18461538

$`2`
 [1] 0.00000000 0.00000000 0.00000000 0.01428571 0.00000000 0.01428571 0.02857143 0.11428571 0.00000000 0.01428571
[11] 0.00000000 0.05714286 0.00000000 0.01428571 0.02857143 0.20000000 0.00000000 0.00000000 0.00000000 0.01428571
[21] 0.00000000 0.01428571 0.02857143 0.17142857 0.01428571 0.00000000 0.00000000 0.02857143 0.01428571 0.02857143
[31] 0.02857143 0.17142857

$`3`
 [1] 0.00000000 0.01408451 0.00000000 0.04225352 0.01408451 0.02816901 0.01408451 0.16901408 0.00000000 0.01408451
[11] 0.00000000 0.00000000 0.01408451 0.00000000 0.02816901 0.19718310 0.00000000 0.00000000 0.00000000 0.04225352
[21] 0.00000000 0.01408451 0.00000000 0.12676056 0.00000000 0.00000000 0.00000000 0.01408451 0.00000000 0.00000000
[31] 0.05633803 0.21126761

What I want to get is 32 vectors of length 3, say: 我想要得到的是长度为3的32个向量,例如:

> 1
[1] 0.00000000 0.0000000 0.0000000
> 2
[1] 0.00000000 0.0000000 0.0000000
> 3
[1] 0.00000000 0.0000000 0.0000000
> 4
[1] 0.01538462 0.01428571 0.04225352

And so on... 等等...

Actually I have a list with more than three vectors inside, so I would like to do it no matter the number of vectors in the list, but those vectors are always of length 32. 实际上,我的列表中包含三个以上的向量,因此无论列表中有多少个向量,我都希望这样做,但是这些向量的长度始终为32。

I have been wondering for a while how to do it... but could not reach a solution... 我一直想知道怎么做...但无法达成解决方案...

Thanks 谢谢

Base R solution: Base R解决方案:

df <- do.call(rbind, my_list)

Then you can grab each set by the column number. 然后,您可以按列号获取每个集合。

We can use transpose from purrr and then map to unlist the nested list to get the list of 32 vector s 我们可以使用来自purrr transpose ,然后mapunlist list嵌套list以获取32个vectorlist

library(tidyverse)
transpose(l1) %>% 
         map(unlist)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM