Dear R users, I have numerous data sets (csv files) saved in the folder which has the same name as individual data. (i.e data x1 saved in x1 folder, data x2 in x2 folder etc) I would like to read in the desired data set name using 'scan' function and assign this inputted value to an object so that it can be used in the 'read.csv' function. For example, x <- scan() 1: 0708 2: Read 1 item dat <- read.csv("D://R//Data//x//x.csv", head=TRUE, sep = ",") Error in file(file, "r") : cannot open the connection In addition: Warning message: In file(file, "r") : cannot open file ('D://R//Data//x//x.csv': No such file or directory In SAS, this can be done by assigning x as a macro variable, is there an equivalent function in R? Moreover, is there any way of using the inputted variable which starts with number 0 (i.e 0102) in the 'read.csv' function? Appreciate for providing your expertise in resolving this problem. [[alternative HTML version deleted]]
Henrique Dallazuanna
2009-Sep-10 00:44 UTC
[R] "Read.csv" in R with dynamic file (1st) argument
Try this: read.csv(sprintf("D://R//Data//%04d//%04d.csv", x, x), header = TRUE) On Wed, Sep 9, 2009 at 9:32 PM, Steven Kang <stochastickang@gmail.com>wrote:> Dear R users, > > > I have numerous data sets (csv files) saved in the folder which has the > same > name as individual data. > (i.e data x1 saved in x1 folder, data x2 in x2 folder etc) > > I would like to read in the desired data set name using 'scan' function and > assign this inputted value to an object so that it can be used in the > 'read.csv' function. > > For example, > > x <- scan() > 1: 0708 > 2: > Read 1 item > > dat <- read.csv("D://R//Data//x//x.csv", head=TRUE, sep = ",") > Error in file(file, "r") : cannot open the connection > In addition: Warning message: > In file(file, "r") : > cannot open file ('D://R//Data//x//x.csv': No such file or directory > > > In SAS, this can be done by assigning x as a macro variable, is there an > equivalent function in R? > > Moreover, is there any way of using the inputted variable which starts with > number 0 (i.e 0102) in the 'read.csv' function? > > Appreciate for providing your expertise in resolving this problem. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
That will not work (or at least doesn't work for me. This does work: fnam<-'thefilename.csv' #or build the name however you like fpath <- 'macintoshhd/users/me/myfolder/ # or whatever you need read.csv(eval(paste(fpath,fnam,sep="")) #worked for me Carl ----------- Try this: read.csv(sprintf("D://R//Data//%04d//%04d.csv", x, x), header = TRUE) On Wed, Sep 9, 2009 at 9:32 PM, Steven Kang <stochastickang at gmail.com>wrote: > Dear R users, > > > I have numerous data sets (csv files) saved in the folder which has the > same > name as individual data. > (i.e data x1 saved in x1 folder, data x2 in x2 folder etc) > > I would like to read in the desired data set name using 'scan' function and > assign this inputted value to an object so that it can be used in the > 'read.csv' function. > > For example, > > x <- scan() > 1: 0708 > 2: > Read 1 item > > dat <- read.csv("D://R//Data//x//x.csv", head=TRUE, sep = ",") > Error in file(file, "r") : cannot open the connection > In addition: Warning message: > In file(file, "r") : > cannot open file ('D://R//Data//x//x.csv': No such file or directory > -- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O