Juan Ceccarelli Arias
2016-Aug-30 15:43 UTC
[R] Loop over rda list files and using the attach function
Hi. I need to loop over rda files. I generated the list of them. That's ok. The problem is that the name of the files are as yyyy_mm (eg 2010_01 is january or 2010, 2016_03 is march of 2016). So, when i try to use the attach function to create a simple table(age, sex) it fails. The only way to attach a file as is using attach(`2016_03`) The text above i have no idea how to declare it in my code below dd=list.files("C:/Users/Me/r", pattern="rda$", full.names=F) for (i in 1:length(dd)) { yyz=load(dd[i]) attach(yyz) table(age, sex) rm(list=yyz) } This is the error it declares the loop: Error in attach(yyz) : file '2013_02' not found Thanks for your help and time [[alternative HTML version deleted]]
Jorge Cimentada
2016-Aug-30 18:01 UTC
[R] Loop over rda list files and using the attach function
Here's the problem: when you load the object and name it yyz, its simply storing the name of the data frame as a string. Naturally, when you you attach the string, it throws an error. The loop actually loads the data frame but inside yyz there's not a data frame. One problem with load() is that you can't save it as an object; it simply loads the data to the chosen environment. A different solution would be to use .Rds, which allows to be saved as an object and makes your loop work. However, you need to make sure your files are saved as .Rds with the saveRDS() function. Here's an example with loadRDS() instead of load(): ## Generate 10 data frames and save it to your working directory as .Rds for (i in 1:10) { x <- data.frame(a=rnorm(10), b=c("Yes","No")) saveRDS(x, file=paste0("data",i,".rds")) # save as RDS rm(x) } dd <- grep(".rds", list.files(), value=T) # vector with data file names for (i in 1:length(dd)) { yyz <- readRDS(dd[i]) # load data.frame and save it to yyz print(with(yyz, table(a, b))) # print the table rm(yyz) # remove data frame } Hope this helps. I found the solution through: https://www.r-bloggers.com/a-better-way-of-saving-and-loading-objects-in-r/ *Jorge Cimentada* *Ph.D. Candidate* Dpt. Ci?ncies Pol?tiques i Socials Ramon Trias Fargas, 25-27 | 08005 Barcelona Office 24.331 [Tel.] 697 382 009 jorge.cimentada at upf.edu http://www.upf.edu/dcpis/ On Tue, Aug 30, 2016 at 5:43 PM, Juan Ceccarelli Arias <jfca283 at gmail.com> wrote:> Hi. > I need to loop over rda files. > I generated the list of them. > That's ok. The problem is that the name of the files are as yyyy_mm (eg > 2010_01 is january or 2010, 2016_03 is march of 2016). > So, when i try to use the attach function to create a simple table(age, > sex) it fails. > The only way to attach a file as is using > attach(`2016_03`) > The text above i have no idea how to declare it in my code below > > > dd=list.files("C:/Users/Me/r", pattern="rda$", full.names=F) > for (i in 1:length(dd)) { > yyz=load(dd[i]) > attach(yyz) > table(age, sex) > rm(list=yyz) > > } > This is the error it declares the loop: > Error in attach(yyz) : file '2013_02' not found > > > Thanks for your help and time > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
Greg Snow
2016-Aug-30 18:36 UTC
[R] Loop over rda list files and using the attach function
You can attach rda files directly with the attach function, no need to load them first (see the what argument in the help for attach). This may do what you want more directly. In general it is better to not use loops and attach for this kind of thing. It is better to store multiple data objects in a list, then use lapply/sapply to apply functions to each element. On Tue, Aug 30, 2016 at 9:43 AM, Juan Ceccarelli Arias <jfca283 at gmail.com> wrote:> Hi. > I need to loop over rda files. > I generated the list of them. > That's ok. The problem is that the name of the files are as yyyy_mm (eg > 2010_01 is january or 2010, 2016_03 is march of 2016). > So, when i try to use the attach function to create a simple table(age, > sex) it fails. > The only way to attach a file as is using > attach(`2016_03`) > The text above i have no idea how to declare it in my code below > > > dd=list.files("C:/Users/Me/r", pattern="rda$", full.names=F) > for (i in 1:length(dd)) { > yyz=load(dd[i]) > attach(yyz) > table(age, sex) > rm(list=yyz) > > } > This is the error it declares the loop: > Error in attach(yyz) : file '2013_02' not found > > > Thanks for your help and time > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. 538280 at gmail.com
ruipbarradas at sapo.pt
2016-Aug-30 18:42 UTC
[R] Loop over rda list files and using the attach function
Hello, Try attach(get(yyz)) Hope this helps, Rui Barradas Citando Juan Ceccarelli Arias <jfca283 at gmail.com>:> Hi. > I need to loop over rda files. > I generated the list of them. > That's ok. The problem is that the name of the files are as yyyy_mm (eg > 2010_01 is january or 2010, 2016_03 is march of 2016). > So, when i try to use the attach function to create a simple table(age, > sex) it fails. > The only way to attach a file as is using > attach(`2016_03`) > The text above i have no idea how to declare it in my code below > > > dd=list.files("C:/Users/Me/r", pattern="rda$", full.names=F) > for (i in 1:length(dd)) { > yyz=load(dd[i]) > attach(yyz) > table(age, sex) > rm(list=yyz) > > } > This is the error it declares the loop: > Error in attach(yyz) : file '2013_02' not found > > > Thanks for your help and time > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Juan Ceccarelli Arias
2016-Aug-30 21:24 UTC
[R] Loop over rda list files and using the attach function
The attach(get(yyz)) option i tried and it worked. The only issue, is that when i'm trying to export the results, it takes a lot of time. Considerably more than Stata. Also, the computer almost collapses for a task which isn't so exhausting for my PC station. Im dubious... On Tue, Aug 30, 2016 at 3:42 PM, <ruipbarradas at sapo.pt> wrote:> Hello, > > Try > > attach(get(yyz)) > > Hope this helps, > > Rui Barradas > > > > > > > > Citando Juan Ceccarelli Arias <jfca283 at gmail.com>: > > Hi. >> I need to loop over rda files. >> I generated the list of them. >> That's ok. The problem is that the name of the files are as yyyy_mm (eg >> 2010_01 is january or 2010, 2016_03 is march of 2016). >> So, when i try to use the attach function to create a simple table(age, >> sex) it fails. >> The only way to attach a file as is using >> attach(`2016_03`) >> The text above i have no idea how to declare it in my code below >> >> >> dd=list.files("C:/Users/Me/r", pattern="rda$", full.names=F) >> for (i in 1:length(dd)) { >> yyz=load(dd[i]) >> attach(yyz) >> table(age, sex) >> rm(list=yyz) >> >> } >> This is the error it declares the loop: >> Error in attach(yyz) : file '2013_02' not found >> >> >> Thanks for your help and time >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posti >> ng-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > > > >[[alternative HTML version deleted]]