Question

Hi i am trying to read in a list of files all at once. I thought of using a lapply function to do this.

tst<-lapply(files, function(x){
  count1 <- read.table( x, h=1, stringsAsFactors=F, row.names=1,col.names=c("gene","counts") )
})

this creates a list of dataframes.Now i need to cbind those dataframes to 1 big data frame i did it like this:

all<-sapply(tst,function(x){
  do.call(cbind,as.data.frame(x))
})

But this creates a matrix instead of a data frame, how can i get the matrix to be converted to a data.frame so that i do not lose my rownames? Also i need to be able to count up the columns per 2 so that all[1] + all[2] will be a column called alltogether or something. Any hints are appreciated.

all the dataframes in tst look like this:

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")
Était-ce utile?

La solution

do.call works on a list of arguments so the following should work:

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top