简体   繁体   English

mapply和两个列表

[英]mapply and two lists

I'm trying to use mapply to combine two lists (A and B). 我正在尝试使用mapply来组合两个列表(A和B)。 Each element is a dataframe. 每个元素都是一个数据帧。 I'm trying to rbind the dataframes in A to the corresponding dataframes in B. The following returns what I would like in combo1: 我正在尝试将A中的数据帧转换为B中的相应数据帧。以下内容返回我在combo1中的内容:

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]])  
}

I'm trying to use mapply to do the same, but I can't get it to work: 我正在尝试使用mapply来做同样的事情,但我无法让它工作:

combo2<-list()
combo2<-mapply("rbind", A, B)

I was hoping someone could please help me 我希望有人可以帮助我

You were very close! 你非常接近!

## 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

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

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