4

I'm trying to create an array of dataframes in r. I have many many csv files, and I want to put them all into separate data frames using a loop.

data1[1] <- "C:/data1.csv"
data1[2] <- "C:/data2.csv"

for(i in 1:2) {
 data2[i] <- data1[i][,-1]
 rownames(data2[i]) <- data1[i][,1] }

Any suggestions on how to make this work?

ebrown1985
  • 65
  • 4
  • 9
  • This is quite confusing, and your sample code is not really a MWE (minimum working example). What is `data1`? And I don't think you want to assign the string `"C:/data1.csv"` to the variable `data1` but rather the data in that file? – vaettchen Jun 19 '13 at 17:15

1 Answers1

4

I would use list.files to list my files by pattern. lapply to loop through the list of files and create a list data.frame with read.csv.

temp <- list.files(pattern="data.*[.]csv",full.names=TRUE)
named.list <- lapply(temp, read.csv)
agstudy
  • 119,832
  • 17
  • 199
  • 261