如何“转置”向量列表?

时间:2019-03-03 15:45:57

标签: r list transpose

我有一个向量列表:

asdf = list(c(1, 2, 3, 4, 5), c(10, 20, 30, 40, 50))

现在我要“转置”它,即获取5对列表而不是5对列表。

更具体地说,我希望结果类似于键入的结果:

transposedAsdf = list(c(1, 10), c(2, 20), c(3, 30), c(4, 40), c(5, 50))

但是我不知道如何实现这一目标。怎么做?

5 个答案:

答案 0 :(得分:4)

transposedAsdf = as.list(as.data.frame(t(as.data.frame(asdf))))
transposedAsdf
$V1
[1]  1 10

$V2
[1]  2 20

$V3
[1]  3 30

$V4
[1]  4 40

$V5
[1]  5 50

答案 1 :(得分:4)

使用data.table的选项

data.table::transpose(asdf)
#[[1]]
#[1]  1 10

#[[2]]
#[1]  2 20

#[[3]]
#[1]  3 30

#[[4]]
#[1]  4 40

#[[5]]
#[1]  5 50 

答案 2 :(得分:4)

使用Python 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 11:07:29) Type "copyright", "credits" or "license" for more information. IPython 7.3.0 -- An enhanced Interactive Python. runfile('/Users/pitosalas/Box Sync/datawork/2019Resubmit/deleteme.py', wdir='/Users/pitosalas/Box Sync/datawork/2019Resubmit') ERROR:root:Invalid alias: The name clear can't be aliased because it is another magic command. ERROR:root:Invalid alias: The name more can't be aliased because it is another magic command. ERROR:root:Invalid alias: The name less can't be aliased because it is another magic command. ERROR:root:Invalid alias: The name man can't be aliased because it is another magic command. hello 软件包的解决方案。

purrr

答案 3 :(得分:3)

这是一种方法:

split(do.call(cbind, asdf), 1:length(asdf[[1]]))
# $`1`
# [1]  1 10
# 
# $`2`
# [1]  2 20
# 
# $`3`
# [1]  3 30
# 
# $`4`
# [1]  4 40
# 
# $`5`
# [1]  5 50

答案 4 :(得分:3)

来自Map的{​​{1}}的一个选项

base R