gpt4 book ai didi

r - mapply 和两个列表

转载 作者:行者123 更新时间:2023-12-04 09:14:10 26 4
gpt4 key购买 nike

我正在尝试使用 mapply 来组合两个列表(A 和 B)。每个元素都是一个数据框。
我正在尝试将 A 中的数据帧绑定(bind)到 B 中的相应数据帧。以下返回我想要的组合 1:

num = 10
A<-list()
B<-list()
for (j in 1:num){
A[[j]] <- as.data.frame(matrix(seq(1:9),3,3))
B[[j]] <- as.data.frame(matrix(seq(10:18),3,3))
}

combo1<-list()
for (i in 1:num){
combo1[[i]] <-rbind(A[[i]], B[[i]])
}

我正在尝试使用 mapply 来做同样的事情,但我无法让它工作:
combo2<-list()
combo2<-mapply("rbind", A, B)

我希望有人可以帮助我

最佳答案

你很亲近!

## Make this a more _minimal_ reproducible example
A <- A[1:2]
B <- B[1:2]

## Override default attempt to reduce results to a vector, matrix, or other array
mapply("rbind", A, B, SIMPLIFY=FALSE)
# [[1]]
# V1 V2 V3
# 1 1 4 7
# 2 2 5 8
# 3 3 6 9
# 4 1 4 7
# 5 2 5 8
# 6 3 6 9
#
# [[2]]
# V1 V2 V3
# 1 1 4 7
# 2 2 5 8
# 3 3 6 9
# 4 1 4 7
# 5 2 5 8
# 6 3 6 9

关于r - mapply 和两个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14329264/

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