[R] importing many csv files into separate matrices

yetik serbest yserbest at prodigy.net
Wed Nov 27 23:39:51 CET 2013


Hi Everyone,
 
I am trying to import many CSV files to their own matrices. Example, alaska_93.csv to alaska. When I execute the following, for each csv.file separately it is successful.
 
singleCSVFile2Matrix <- function(x,path) {
 assign(gsub(pattern=".csv",x,replacement=""),read.csv(paste(path,x,sep="")))
}
 
when I try to include it in a loop in another function (I have so many csv files to import), it doesn't work. I mean the following function doesn't do it.
 
loadCSVFiles_old <- function(path) {
 x <- list.files(path)
 for (i in 1:length(x)) {
  assign(gsub(pattern=".csv",x[i],replacement=""),read.csv(paste(path,x[i],sep="")))
  }
}
 
Instead, if I execute the foor loop in the command line, it works. I am puzzled. Appreciate any help.
 
thanks
yetik



More information about the R-help mailing list