3

I have numerous tab delimited .txtfiles named as "abcd001.txt, abcd002.txt".... stored in a directory. I was able to import them using the following code, (whereby default directory is sames as data files directory). Its three column, all numeric type data

filenames <- list.files(path=".",pattern="abcd+.*txt")

#list of data in R
names <-substr(filenames,1,6)


for(i in names){
    filepath <- file.path(".",paste(i,".txt",sep=","))
    assign(i, read.table(filepath,
     colClasses=c("numeric"),
    sep = "\t"))
}

The code itself hasnt returned any error. My doubt is How can I access data which is being loaded? How to access say the data of the file abcd011.txt which should be three column data

The commands:names[3] just returns the file number 000002 But no data.

The code here is similar to one here: Read multiple CSV files into separate data frames.

Community
  • 1
  • 1
Asunius
  • 31
  • 1
  • 2
  • 1
    Your troubles illustrate nicely why using `assign` to store a collection of data does not work smoothly. If you want to get the contents of the variable, just use `get`. Alternatively, just put the data in a list, see my answer for details. – Paul Hiemstra Oct 29 '12 at 12:29

1 Answers1

8

I would recommend to put the results of read.table either in a list, or in one bit data.frame. In addition, I would recommend using apply style loops here, either standard R (lapply) or plyr. I prefer using plyr, so my examples will be using that package. An example:

## Read into a list of files:
filenames <- list.files(path=".",pattern="abcd+.*txt")
list_of_data = llply(filenames, read.table, 
                                        colClasses = c("numeric"), sep = "\t")

Accessing the data can now be done using:

list_of_data[[1]]
list_of_data[["abcd1.txt"]]

Or you could read the contents of the files into one big data.frame:

big_data = ldply(filenames, read.table, 
                             colClasses = c("numeric"), sep = "\t"))

Accessing the contents of a file can now be done using:

big_data[big_data$variable == "abcd1.txt",]
Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • Thanks! The command `llply` worked. But with `ldply` and using `big_data[big_data$variable == "abcd1.txt"]` it returned `data frame with 0 coulmns and 203400 rows`. Why? – Asunius Oct 29 '12 at 13:38
  • That is hard to say without example output. I guess that `"abcd1.txt"` is a non existent entry in the `variable` column. – Paul Hiemstra Oct 29 '12 at 16:51