[R] Bind element of List of matrix or data.frame or list
来源:互联网 发布:淘宝女士运动套装 编辑:程序博客网 时间:2024/06/11 10:20
- Alternative solutions for list of matrix or dataframe
- Alternative solutions for list of 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 1result1.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 Rlibrary(plyr)result1.2.1 <- ldply(myList1, rbind)head(result1.2.1)###Error: All inputs to rbind.fill must be data.framesresult1.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.framelibrary(data.table)###Error in rbindlist(myList1) : Item 1 of list input is not a data.frame, data.table or listresult1.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.timelibrary(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 listmyList3 <- 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 listtempList <- 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
- [R] Bind element of List of matrix or data.frame or list
- R语言中的matrix(矩阵),list(列表),data.frame(数据框)总结
- List of KNX Open Source or free software
- R语言convesio of json files to csv or R data format
- List the maximum or minimum of the elements at each position of two arrays
- System.Data - System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the r
- Get middle element of a linked-list
- fundamental of D3: Bind Data and element Selection
- R软件学习笔记(vector,Array,List,Data Frame)
- R软件学习笔记(vector,Array,List,Data Frame)
- R-list-to-data-frame列表转数据框
- getWritableDatabase called recursively;bind or column index out of range
- SQLiteException: bind or column index out of range
- SQLiteException: bind or column index out of range
- Quick list of useful R packages
- A list of books on general-purpose algorithms for the practical programmer (or 'software engineer')
- which is best to use ? List or Dictionary ? For efficiency of code execution.
- TypeError: view must be a callable or a list/tuple in the case of include()
- mybatis常用配置
- 网卡故障:弹出界面eth0: 错误:没有找到合适的设备:没有找到可用于链接System eth0 的
- SET character_set_connection=utf8
- Error:程序包android.support.v4.app不存在;Error:找不到类FragmentActivity
- Android之AIDL使用详解
- [R] Bind element of List of matrix or data.frame or list
- 颜色(一):UIColor
- CentOS6.4下安装MySql
- 码农路,渐行渐远
- iOS初学快捷键
- 数据库第二范式
- 文章标题
- Maven Pom 文件中的隐式依赖导致Jar冲突
- [俄勒冈大学]养成Python写作习惯