[R] Bind element of List of matrix or data.frame or list

1. Alternative solutions for list of matrix or data.frame

If I have a list of matrix or data.frame, we can use the following ways to bind the rows of all elements.

Firstly, I generate toy data

myList1 <- list(matrix(rnorm(2*3), ncol=2),
                matrix(rnorm(2*3), ncol=2),
                matrix(rnorm(2*3), ncol=2),
                matrix(rnorm(2*3), ncol=2))
myList2 <- list(as.data.frame(matrix(rnorm(2*3), ncol=2)),
                as.data.frame(matrix(rnorm(2*3), ncol=2)),
                as.data.frame(matrix(rnorm(2*3), ncol=2)),
                as.data.frame(matrix(rnorm(2*3), ncol=2)))

Now I list the alternative solutions

# solution 1
result1.1 <- do.call(rbind, myList1)
head(result1.1)
result1.2 <- do.call(rbind, myList2)
head(result1.2)

# solution 2
## plyr: the split-apply-combine paradigm for R
library(plyr)
result1.2.1 <- ldply(myList1, rbind)
head(result1.2.1)
###Error: All inputs to rbind.fill must be data.frames
result1.2.2 <- rbind.fill(myList1)
head(result1.2.2)
result2.2.1 <- ldply(myList2, rbind)
head(result2.2.1)
result2.2.2 <- rbind.fill(myList2)
head(result2.2.2)

# solution 3
## data.table: Enhanced data.frame
library(data.table)
###Error in rbindlist(myList1) : Item 1 of list input is not a data.frame, data.table or list
result1.3 <- rbindlist(myList1)
head(result1.3)
result2.3 <- rbindlist(myList2)
head(result2.3)

From the codes, we can see that

  • for matrix, only do.call(rbind, ), ldply can work.
  • for data.frame, all do.call(rbind, ), ldply, rbind.fill, rbindlist can work.

And now benchmark for all solutions

# benchmark
## benchmark: a simple wrapper around system.time
library(rbenchmark)
benchmark(do.call(rbind, myList2), ldply(myList2, rbind), rbind.fill(myList2), rbindlist(myList2))

2. Alternative solutions for list of list

#############list of list#############################

# generate list of list
myList3 <- vector("list", 4)
for(i in 1:4){
  myList3[[i]] <- vector("list", 2)
  for(j in 1:2){
    myList3[[i]][[j]] <- rnorm(3)
  }
}

# bind each element of outer list
tempList <- lapply(myList3, function(z)do.call(rbind,z))
## selection operator "["
bind.ith.rows <- function(i) do.call(rbind, lapply(tempList, "[", i, TRUE))
nr <- nrow(tempList[[1]])
lapply(1:nr, bind.ith.rows)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值