0

I know I have a simple problem here, but I can't seem to get it. I have multiple .txt files that I would like to use read.table on by looping, in order for each .txt file to be its own data.frame. The issue comes in regards to being able to read in each .txt file name with its own unique variable name

For example:

path = "dir" #my directory 
filenames <- list.files(path) 

for (i in 1:length(filenames))
  {

  ID <- filenames[i]
  ID <- paste(ID, "average_table", sep = "_")

  read.table(filenames[i], header=TRUE) # error about "cannot open connection" and "cannot open file ID: no such file or directory"

  } 

For example, if I have sub1.txt, sub2.txt, sub3.txt ... I would like to simply read each one in as a data.frame, each with having the variable name of its .txt file with the "average_table" added at the end (e.g. first loop through with sub1.txt would lead to a data.frame variable of sub1_average_table).

I've used lapply, but this of course creates a list, which is not what I want. What would be the best way then to read in each file, while naming its corresponding data.frame to the .txt file name? Thank you.

pdhami
  • 65
  • 1
  • 7
  • 1
    Why are you using both `filenames[i]` and the `file` argument? Also, if you don't want a list then what do you want? A list is the best way to go here. I would do `sapply(filenames, read.table, header = TRUE)` and then you have a named list of data frames. The names being the file names they came from – Rich Scriven Apr 23 '16 at 17:09
  • Sorry about the use of file, that was a mistake. I was hoping to import each .txt file as its own unique data.frame into R. My goal was to ultimately have these different data.frames, then create a new one which contains the averages of the individual data.frames element by element. – pdhami Apr 23 '16 at 17:19
  • You should reconsider the "anti-list" sentiment. Lists are a great way to organize and work with mulitple data.frames. See the following answer for strong motivation in this regard: [how to make a list of data.frames](http://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames/24376207#24376207) – lmo Apr 23 '16 at 20:02

1 Answers1

0

You can use assign to get the solution you are asking for.

Moreover: In order to get rid of the problem that read.table could not find the files, you should use file.path to combine your path with the names of the files you want to open. (Your present solution makes read.table look for the files in the active working directory.)

path = "dir" #my directory 
filenames <- list.files(path) 

result_names <- structure(
    .Data = gsub(
        pattern = "\\.txt",
        replacement = "_average_table",
        x = filenames),
    .Names = filenames)


for (.file in filenames) {
    assign(
        x = result_names[.file],
        value = read.table(
            file = file.path(path, .file),
            header = TRUE),
        envir = .GlobalEnv)
}

For the record: I think it would be much better to use a solution based on sapply, as mentioned in the comment of @Richard Scriven. That solution would only add one object to your global workspace, and you can use sub-setting if you for some reason would like to inspect one particular data frame.