0

I have used the following code to read multiple .csv files in R:

    Assembly<-t(read.table("E:\\test\\exp1.csv",sep="|",header=FALSE,col.names=c("a","b","c","d","Assembly","f"))[1:4416,"Assembly",drop=FALSE])
    Top1<-t(read.table("E:\\test\\exp2.csv",sep="|",header=FALSE,col.names=c("a","b","c","d","Top1","f"))[1:4416,"Top1",drop=FALSE])
    Top3<-t(read.table("E:\\test\\exp3.csv",sep="|",header=FALSE,col.names=c("a","b","c","d","Top3","f"))[1:4416,"Top3",drop=FALSE])
    Top11<-t(read.table("E:\\test\\exp4.csv",sep="|",header=FALSE,col.names=c("a","b","c","d","Top11","f"))[1:4416,"Top11",drop=FALSE])
    Assembly1<-t(read.table("E:\\test\\exp5.csv",sep="|",header=FALSE,col.names=c("a","b","c","d","Assembly1","f"))[1:4416,"Assembly1",drop=FALSE])
    Area<-t(read.table("E:\\test\\exp6.csv",sep="|",header=FALSE,col.names=c("a","b","c","d","Area","f"))[1:4416,"Area",drop=FALSE])

    data<-rbind(Assembly,Top1,Top3,Top11,Assembly1,Area)

So the entire data is in the folder "test" in E drive. Is there a simpler way in R to read multiple .csv data with a couple of lines of code or some sort of function call to substitute what has been made above?

SegFault
  • 2,526
  • 4
  • 21
  • 41
  • See [this](http://stackoverflow.com/questions/11433432/importing-multiple-csv-files-into-r) answer. – R. Schifini Feb 07 '16 at 04:38
  • I need only the 5th column, and I want to name that column. The above answer is'nt clear on that. – Shreyas Karnick Feb 07 '16 at 05:33
  • Maybe I'm not understanding what you are doing, but `rbind`ing dataframes with different conames doesn't work: _It then takes the classes of the columns from the first data frame, and matches columns by name (rather than by position)._ – vaettchen Feb 07 '16 at 06:28

1 Answers1

0

(Untested code; no working example available) Try: Use the list.files function to generate the correct names and then use colClasses as argument to read.csv to throw away the first 4 columns (and since that vector is recycled you will alss throw away the 6th column):

lapply(list.files("E:\\test\\", patt="^exp[1-6]"), read.csv, 
                       colClasses=c(rep("NULL", 4), "numeric"), nrows= 4416)

If you want this to be returned as a dataframe, then wrap data.frame around it.

IRTFM
  • 258,963
  • 21
  • 364
  • 487