I am looking into importing multiple csv files in R
temp = list.files(pattern="*.csv")
for (i in 1:length(temp)) assign(temp[i], read.csv(temp[i]))
however
temp[1] returns "something.csv"
.......
is there something wrong with my code?
I am looking into importing multiple csv files in R
temp = list.files(pattern="*.csv")
for (i in 1:length(temp)) assign(temp[i], read.csv(temp[i]))
however
temp[1] returns "something.csv"
.......
is there something wrong with my code?
No need for for
looping in R when we have sapply
(and the other *apply
functions).
In this case, with no further arguments sapply
returns a named list of data frames, which I will call read.all
.
> temp <- list.files(pattern = "*.csv")
> read.all <- sapply(temp, read.csv)
Looking at read.all
shows that it is a named list of data frames.
You can then access the individual data frames by file name with
> read.all[["filename.csv"]] ## or read.all["filename.csv"]
or with the $
operator
> read.all$filename.csv
SOLUTION:
Fixes: 1) must use double-[[]] to get the individual list element 2) don't use assign()
So either:
for (i in 1:length(temp)) { temp[[i]] <- read.csv(temp[i]) }
or, if you don't want to overwrite the temp variable:
df = c(rep(data.frame(), length(temp))) # list of empty dataframe
for (i in 1:length(temp)) { df[[i]] <- as.list(read.csv(temp[i])) }
There were two separate mistakes in your original code:
using single [] instead of double [[]]. Single [] gives you a list slice containing one element (not what you want to assign to), instead of just that actual element.
assign
is not doing what you think it's doing, as @G-Grothendieck said.
You simply want to do temp[[i]] <- read.csv(temp[i])
But what you're actually doing is assigning to the variable whose name is contained in temp[i]. So if temp[i] is 'whosyour.csv', you're actually creating and assigning to a variable with that name, rather than assigning to temp[i] itself:
whosyour.csv <- read.csv('whosyour.csv') # NOT WHAT YOU WANTED!
Try this:
temp <- list.files(pattern = "*.csv")
## for individual files
dataset <- lapply(temp,FUN=function(files){read.table(files,header=TRUE, sep=",")})
dataset[1] ## for specific files of interest, OR
## If your CSV column structure is same across all csv's bind them all into 1 file
dataset <- do.call("rbind",lapply(temp,FUN=function(files){read.table(files,header=TRUE, sep=",")}))