1

I have multiple text files (tab-delimited) generated from the same software. I initially used a loop with assign function to create variables dynamically and store them separately with the read.table function. This resulted in too many variables and was obviously time-consuming to apply operations on separate files.

I came across the lapply and fread method shown in the code below.

I don't need to merge them and they need to be separate data frames so I can compare values in the files. Using the lapply function, this was possible but the file names were not retained in any way. I found the following code from How to import multiple .csv files at once? that helped me with it. It has multiple lines and I was wondering whether there is a one-line solution for this.

foo <- function(fname){
  fread(fname, skip = 5, header = TRUE, sep = " ") %>% 
   mutate(fn = fname)
}

all <- lapply(files, FUN = foo)

Alternatively, how do I access the specific iteration in lapply?

K_D
  • 147
  • 8
  • Not clear, you can access the datasets with `[[` ie. `all[[1]]`, `all[[2]]` – akrun Dec 04 '19 at 13:30
  • Thanks! I am aware that the datasets can be accessed with all[[1]], etc. but I would like to somehow add the original filenames to the datasets so I know which dataset in the Large List i.e. all, corresponds to which file on the disk. Hence, the mutate(fn = fname). Hope thats clear – K_D Dec 04 '19 at 13:49

2 Answers2

1

We can use setNames

all <- setNames(lapply(files, foo), files)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    This works well! The previous solution, though bulky as it added a column with filenames in all rows, was decent but this is neater. It reduced the size of the list. Thanks! – K_D Dec 04 '19 at 14:40
0

We can also make a general function that will set the names as the files are imported:

import_with_names <- function(files){

    loaded <- list()
    for (fname in files){
        loaded[[fname]] <- fread(fname, skip = 5, header = TRUE, sep = " ")
    }
    return(loaded)
}

all <- import_with_names(files)

You can then call them by using all[[file_name]]

Jamie
  • 174
  • 1
  • 10
  • Thankyou! Could you let me know where does fread get the fname from? In my case fname was the argument in the function – K_D Dec 04 '19 at 15:00