On Wed, Dec 4, 2013 at 6:53 PM, Greg Snow <538...@gmail.com> wrote: > As you have noticed, using assign is not simple, and your approach has > potential to cause even more problems even if you get it working. > Here is another approach: > > loadCSVfiles <- function(path) { > x <- list.files(path, full.names=TRUE) > out <- lapply( x, read.csv ) > names(out) <- sub(pattern="\\.csv$", replacement="", x) > out > } > > then run: > > mydata <- loadCSVfiles("/my/path")
I fully agree with this; instead of messing around with assign() - ending up using assign() is often a good indicator that there is another better way to do it. BTW, read.csv() returns a data.frame (not a matrix) just as read.table() do. An alternative to the above loadCSVfiles() function, is to use the R.filesets package, e.g. library("R.filesets") ds <- TabularTextFileSet$byPath("/my/path", pattern="[.]csv$") mydata <- lapply(ds, FUN=readDataFrame) That also sets the names by the filenames w/out the extension. If one don't like that style, the same thing can be achieved by: library("R.filesets") files <- dir(path="/my/path", pattern="[.]csv$", full.names=TRUE) mydata <- readDataFrame(files, combineBy=NULL) /Henrik > > and mydata will be a list with all of your data objects with the > desired names. You can do things like: > > plot(mydata$alaska) > > or > > with(mydata, plot(alaska)) > > or > > lapply( mydata, plot ) > > etc. > > This approach does not place the individual objects into the global > workspace, but that is a good thing. > > On Wed, Nov 27, 2013 at 3:39 PM, yetik serbest <yserb...@prodigy.net> wrote: >> 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 >> >> ______________________________________________ >> R-help@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. > > > > -- > Gregory (Greg) L. Snow Ph.D. > 538...@gmail.com > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.