user2806363
user2806363

Reputation: 2593

Undefined columns selected error in R while reading multiple files

I have lot's of files in my directoy and I want to read all files and select the second columns of them and put those columns as rows of a matrix, but I face with strange error.

would anybody help me to figure out, what's going wrong with my code ?

Here is my effort:

#read all files in one directoy into R and select desired column
nm <- list.files(path="April/mRNA_expression/")
Gene_exp<-do.call(rbind, lapply(nm, function(x) read.table(file=x,header=TRUE, sep= ",")[, 2]))
save(Gene_exp, file="Path to folder")

The error I get is :

## Error in `[.data.frame`(read.table(file = x, header = TRUE, sep = ""),  :
## undefined columns selected*

To check that, really my files have 2 columns I did this :

b <- read.table("A.genes.normalized_results", sep="")
dim(b)
## [1] 20532     2

My text file Looks like this :

gene_id normalized_count
?|100130426 0.0000
?|100133144 10.6340
?|100134869 5.6790
?|10357 106.4628
?|10431 710.8902
?|136542    0.0000
?|155060    132.2883
?|26823 0.5098
?|280660    0.0000
?|317712    0.0000
?|340602    0.0000
?|388795    1.2745
?|390284    5.3527
?|391343    2.5489
?|391714    0.7647
?|404770    0.0000
?|441362    0.0000

Upvotes: 1

Views: 1109

Answers (1)

ilir
ilir

Reputation: 3224

The better solution would be to only import the second column when reading it. Use the colClasses argument to completely skip the first:

Gene_exp<-do.call(rbind, lapply(nm, function(x) read.delim(file=x,header=TRUE, colClasses=c('NULL', 'character'))))

I am assuming the second column is character. Change it to the appropriate class if you need to.

Upvotes: 1

Related Questions