I have a list of list with common structure
我有一个具有公共结构的列表
require(data.table)
l <- list(a1 = list(b=data.table(rnorm(3)), c=data.table(rnorm(3)), d=data.table(rnorm(3))),
a2 = list(b=data.table(rnorm(3)), c=data.table(rnorm(3)), d=data.table(rnorm(3))))
Sometimes it is easier for lapply
to change the structure to go from a 2x3 list to a 3x2 list like:
有时,lapply更容易将结构从2x3列表改为3x2列表,比如:
+a1---b +b---a1
---c ---a2
---d +c---a1
+a2---b to ---a2
---c +d---a1
---d ---a2
Is there an idiomatic way to do this ?
有没有一种惯用的方法来做到这一点?
Can it be done without copying over all the tables (that may be very big) ?
是否可以在不复制所有表(可能非常大)的情况下完成?
1 个解决方案
#1
3
I think purrr::transpose()
is what you are looking for.
我认为purrr::转置()是你正在寻找的。
(ll = purrr::transpose(l))
# $b
# $b$a1
# V1
# 1: -0.9615584
# 2: -0.8849469
# 3: 0.4831375
#
# $b$a2
# V1
# 1: 0.4634884
# 2: -0.7079083
# 3: -0.4366986
#
#
# $c
# $c$a1
# V1
# 1: 0.4710617
# 2: -0.4927592
# 3: -1.3484420
#
# $c$a2
# V1
# 1: -0.4547821
# 2: 0.5752723
# 3: 0.6272826
#
#
# $d
# $d$a1
# V1
# 1: 0.80827129
# 2: -0.03640465
# 3: -1.89417912
#
# $d$a2
# V1
# 1: 0.1844341
# 2: 0.4557670
# 3: -0.5714462
Checking the dimensions:
检查维度:
length(ll)
# [1] 3
sapply(ll, length)
# b c d
# 2 2 2
It looks like its implemented in C so my guess is efficiency is fairly well-optimized and unnecessary copying is minimized.
它看起来像是在C语言中实现的,所以我认为效率是相当优化的,不必要的复制被最小化了。
#1
3
I think purrr::transpose()
is what you are looking for.
我认为purrr::转置()是你正在寻找的。
(ll = purrr::transpose(l))
# $b
# $b$a1
# V1
# 1: -0.9615584
# 2: -0.8849469
# 3: 0.4831375
#
# $b$a2
# V1
# 1: 0.4634884
# 2: -0.7079083
# 3: -0.4366986
#
#
# $c
# $c$a1
# V1
# 1: 0.4710617
# 2: -0.4927592
# 3: -1.3484420
#
# $c$a2
# V1
# 1: -0.4547821
# 2: 0.5752723
# 3: 0.6272826
#
#
# $d
# $d$a1
# V1
# 1: 0.80827129
# 2: -0.03640465
# 3: -1.89417912
#
# $d$a2
# V1
# 1: 0.1844341
# 2: 0.4557670
# 3: -0.5714462
Checking the dimensions:
检查维度:
length(ll)
# [1] 3
sapply(ll, length)
# b c d
# 2 2 2
It looks like its implemented in C so my guess is efficiency is fairly well-optimized and unnecessary copying is minimized.
它看起来像是在C语言中实现的,所以我认为效率是相当优化的,不必要的复制被最小化了。