Dear helpeRs,
I am attempting to read in a series of csv files so I can bind them
into one large dataframe. I have written the following script:
test <- list.files(".", pattern = "csv") #lline 1
imp <- list() #line 2
for (i in 1:length(test)) { #line 3
imp[i] <- read.csv(test[i]) #line 4
} #line 5
works <- do.call(rbind, imp) #line 6
write.csv(works, "allmet.csv") #line 7
This script has an error at line 4. imp[i] contains only the value
of the first element of test[i]; in other words, every element of
imp[i] equals test[i] [1,1]. Otherwise, the script works. Could
someone please enlighten me as to the correct syntax for line 4?
Thank you in advance.
Toby
Toby Gass wrote:> Dear helpeRs, > > I am attempting to read in a series of csv files so I can bind them > into one large dataframe. I have written the following script: > > test <- list.files(".", pattern = "csv") #lline 1 > imp <- list() #line 2 > for (i in 1:length(test)) { #line 3 > imp[i] <- read.csv(test[i]) #line 4 > } #line 5 > works <- do.call(rbind, imp) #line 6 > write.csv(works, "allmet.csv") #line 7 > > This script has an error at line 4. imp[i] contains only the value > of the first element of test[i]; in other words, every element of > imp[i] equals test[i] [1,1]. Otherwise, the script works. Could > someone please enlighten me as to the correct syntax for line 4?Just use lapply instead of a loop, easier syntax. imp <- lapply(test, read.csv) do.call(rbind, imp)