您好我正在尝试一次读取文件列表。我想过使用lapply函数来做到这一点。
tst<-lapply(files, function(x){
count1 <- read.table( x, h=1, stringsAsFactors=F, row.names=1,col.names=c("gene","counts") )
})
这会创建一个dataframes的列表。现在我需要将这些数据帧绑定到1个大数据帧,我这样做了:
all<-sapply(tst,function(x){
do.call(cbind,as.data.frame(x))
})
但是这会创建一个矩阵而不是数据框,我怎样才能将矩阵转换为data.frame以便我不会丢失我的rownames?此外,我需要能够计算每2列的数量,以便所有[1] +所有[2]将是一个名为alltogether或列的列。任何提示都表示赞赏。
tst中的所有数据帧如下所示:
tst[1]<-structure(list(counts = c(0L, 0L, 3L, 0L, 2L, 0L)), .Names = "counts", row.names = c("1/2-SBSRNA4","A1BG", "A1BG-AS1", "A1CF", "A2LD1", "A2M"), class = "data.frame")
tst[2]<-structure(list(counts = c(0L, 0L, 3L, 0L, 2L, 0L)), .Names = "counts", row.names = c("1/2-SBSRNA4","A1BG", "A1BG-AS1", "A1CF", "A2LD1", "A2M"), class = "data.frame")
答案 0 :(得分:2)
do.call
适用于参数列表,因此以下内容应该有效:
tst <- list()
tst[[1]]<-structure(list(counts = c(0L, 0L, 3L, 0L, 2L, 0L)), .Names = "counts", row.names = c("1/2-SBSRNA4","A1BG", "A1BG-AS1", "A1CF", "A2LD1", "A2M"), class = "data.frame")
tst[[2]]<-structure(list(counts = c(0L, 0L, 3L, 0L, 2L, 0L)), .Names = "counts", row.names = c("1/2-SBSRNA4","A1BG", "A1BG-AS1", "A1CF", "A2LD1", "A2M"), class = "data.frame")
> do.call(cbind.data.frame, tst)
counts counts
1/2-SBSRNA4 0 0
A1BG 0 0
A1BG-AS1 3 3
A1CF 0 0
A2LD1 2 2
A2M 0 0