[R] Loop over rda list files and using the attach function

Jorge Cimentada cimentadaj at gmail.com
Tue Aug 30 20:01:12 CEST 2016

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

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:

*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

On Tue, Aug 30, 2016 at 5:43 PM, 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.

	[[alternative HTML version deleted]]

More information about the R-help mailing list