4

I tried looking for the answer to this but I could only find how to create individual data frames from csv files. I have many csv files in my working directory, and instead of assigning them to individual data frames by

frame1 = read.csv(filepath)

I would like have them contained in a list of data frames that I could do operations on. This would obviously require a loop over the files in dir(), but I am not sure of the syntax. In java I would do List.add() for each element.

Thank you

raj rajaratnam
  • 311
  • 2
  • 3
  • 8

1 Answers1

8

Something like the following might be helpful.

my.path <- list("filepath1", "filepath2", "filepath3")
my.data <- list()
for (i in 1:length(my.path)){
    my.data[[i]] <- read.csv(my.path[[i]])
}

my.data is a list containing data frames

EDIT

The previous answer shows how to dynamically assign elements of a list. However, a more compact way to achieve your task would be

my.path <- list("filepath1", "filepath2", "filepath3")
my.data <- lapply(my.path, read.csv)
QuantIbex
  • 2,324
  • 1
  • 15
  • 18