Loop over rda list files and using the attach function

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Loop over rda list files and using the attach function

jfca283
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Loop over rda list files and using the attach function

Jorge Cimentada
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
[hidden email]
http://www.upf.edu/dcpis/



On Tue, Aug 30, 2016 at 5:43 PM, Juan Ceccarelli Arias <[hidden email]>
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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Loop over rda list files and using the attach function

glsnow
In reply to this post by jfca283
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
<[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.
[hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Loop over rda list files and using the attach function

Rui Barradas
In reply to this post by jfca283
Hello,

Try

attach(get(yyz))

Hope this helps,

Rui Barradas







Citando Juan Ceccarelli Arias <[hidden email]>:

> 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Loop over rda list files and using the attach function

jfca283
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, <[hidden email]> wrote:

> Hello,
>
> Try
>
> attach(get(yyz))
>
> Hope this helps,
>
> Rui Barradas
>
>
>
>
>
>
>
> Citando Juan Ceccarelli Arias <[hidden email]>:
>
> 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]]
>>
>> ______________________________________________
>> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Loop over rda list files and using the attach function

jfca283
Hi
I want to comment something.
When i added the detach(get(yyz)) the RAM consumption was considerable
reduced.
So, i want to declare this issue as solved and thank you all for your
assistance.
Good luck to all.

On Tue, Aug 30, 2016 at 6:24 PM, Juan Ceccarelli Arias <[hidden email]>
wrote:

> 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, <[hidden email]> wrote:
>
>> Hello,
>>
>> Try
>>
>> attach(get(yyz))
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>>
>>
>>
>>
>>
>>
>> Citando Juan Ceccarelli Arias <[hidden email]>:
>>
>> 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]]
>>>
>>> ______________________________________________
>>> [hidden email] 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]]

______________________________________________
[hidden email] 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.