gpt4 book ai didi

r - 转置 R 中列表的元素

转载 作者:行者123 更新时间:2023-12-02 01:49:54 25 4
gpt4 key购买 nike

我有这个列表:

list1 <- list(A = c("A1", "B1", "C1"), B = c("A2", "B2", "C2"), D = c("A3", "B3", "C3"))
list2 <- list(A = c("D1", "E1", "F1"), B = c("D2", "E2", "F2"), D = c("D3", "E3", "F3"))
list3 <- list(A = c("G1", "H1", "I1"), B = c("G2", "H2", "I2"), D = c("G3", "H3", "I3"))
my_list <- list(list1, list2, list3)

我想把它变成这个列表:

list1a <- list(A = c("A1", "B1", "C1"), B = c("D1", "E1", "F1"), D = c("G1", "H1", "I1"))
list2a <- list(A = c("A2", "B2", "C2"), B = c("D2", "E2", "F2"), D = c("G2", "H2", "I2"))
list3a <- list(A = c("A3", "B3", "C3"), B = c("D3", "E3", "F3"), D = c("G3", "H3", "I3"))
my_new_list <- list(list1a, list2a, list3a)

使用基本 R 函数的最有效解决方案是什么?

最佳答案

一个选项

sapply(1:length(my_list),function(x){lapply(my_list,"[[",x)},simplify=F)

[[1]]
[[1]][[1]]
[1] "A1" "B1" "C1"

[[1]][[2]]
[1] "D1" "E1" "F1"

[[1]][[3]]
[1] "G1" "H1" "I1"
...

关于r - 转置 R 中列表的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70461854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com