subsetting ls() as per class...

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

subsetting ls() as per class...

akshay kulkarni
dear memebers,
                               I am using R in AWS linux instance for my research. I want to remove certain objects from the global environment  to reduce my EBS cost..for example, I want to remove all objects of class "xts", "zoo". Is there any way to automate this, instead of removing the objects one by one?

Basically, I want to subset  ls() according to class, and then remove that subset by using rm function.

I got to know about mget in SO, but that is not working in my case....

Also, all the above objects end with ".NS".  I came to know that you can remove objects starting with a certain pattern; is there any way to remove objects ending in a certain pattern?

very many thanks for your time and effort...
yours sincerely,
AKSHAY M KULKARNI

        [[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: subsetting ls() as per class...

plangfelder
Looking at ?rm, my solution would be something like

rm(list = grep("\\.NS$", ls(), value = TRUE))

But test it since I have not tested it.

Peter


On Fri, Jul 27, 2018 at 10:58 PM akshay kulkarni <[hidden email]> wrote:

>
> dear memebers,
>                                I am using R in AWS linux instance for my research. I want to remove certain objects from the global environment  to reduce my EBS cost..for example, I want to remove all objects of class "xts", "zoo". Is there any way to automate this, instead of removing the objects one by one?
>
> Basically, I want to subset  ls() according to class, and then remove that subset by using rm function.
>
> I got to know about mget in SO, but that is not working in my case....
>
> Also, all the above objects end with ".NS".  I came to know that you can remove objects starting with a certain pattern; is there any way to remove objects ending in a certain pattern?
>
> very many thanks for your time and effort...
> yours sincerely,
> AKSHAY M KULKARNI
>
>         [[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: subsetting ls() as per class...

Jeff Newmiller
In reply to this post by akshay kulkarni
You can extract the names into a character vector with ls and then use grep(..., values=TRUE ) to select which ones you want to remove, and then pass that list to rm.

However, due to the way R handles memory you are unlikely to see much savings by doing this. I would recommend focusing on creating a script or series of scripts that can allow you to re-create your analysis, and then restarting R whenever you are ready to reduce memory usage. This will have the side benefit of leaving you with a verified-complete record of how your analysis was done.

On July 27, 2018 10:58:36 PM PDT, akshay kulkarni <[hidden email]> wrote:

>dear memebers,
>I am using R in AWS linux instance for my research. I want to remove
>certain objects from the global environment  to reduce my EBS cost..for
>example, I want to remove all objects of class "xts", "zoo". Is there
>any way to automate this, instead of removing the objects one by one?
>
>Basically, I want to subset  ls() according to class, and then remove
>that subset by using rm function.
>
>I got to know about mget in SO, but that is not working in my case....
>
>Also, all the above objects end with ".NS".  I came to know that you
>can remove objects starting with a certain pattern; is there any way to
>remove objects ending in a certain pattern?
>
>very many thanks for your time and effort...
>yours sincerely,
>AKSHAY M KULKARNI
>
> [[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.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[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: subsetting ls() as per class...

Henrik Bengtsson-5
The ll() function of R.oo returns a data.frame with various attributes that
you can subset on, e.g.

> subset(R.oo::ll(), data.class %in% c("zoo", "xts"))
       member data.class dimension objectSize
2          fz        zoo        10       1344
4  sample.xts        xts  c(180,4)      10128
5           x        zoo         5        528
6          x1        zoo         5        880
7          x2        zoo         5        496
9           y        zoo         5       1040
11          z        zoo    c(5,3)       1184
12         z0        zoo         0        448
13         z2        zoo    c(4,3)        904
14        z20        zoo    c(4,0)        616
15         z3        zoo         8        528
16         z4        zoo         5        592
17         z5        zoo         5        792

Henrik

On Sat, Jul 28, 2018, 08:22 Jeff Newmiller <[hidden email]> wrote:

> You can extract the names into a character vector with ls and then use
> grep(..., values=TRUE ) to select which ones you want to remove, and then
> pass that list to rm.
>
> However, due to the way R handles memory you are unlikely to see much
> savings by doing this. I would recommend focusing on creating a script or
> series of scripts that can allow you to re-create your analysis, and then
> restarting R whenever you are ready to reduce memory usage. This will have
> the side benefit of leaving you with a verified-complete record of how your
> analysis was done.
>
> On July 27, 2018 10:58:36 PM PDT, akshay kulkarni <[hidden email]>
> wrote:
> >dear memebers,
> >I am using R in AWS linux instance for my research. I want to remove
> >certain objects from the global environment  to reduce my EBS cost..for
> >example, I want to remove all objects of class "xts", "zoo". Is there
> >any way to automate this, instead of removing the objects one by one?
> >
> >Basically, I want to subset  ls() according to class, and then remove
> >that subset by using rm function.
> >
> >I got to know about mget in SO, but that is not working in my case....
> >
> >Also, all the above objects end with ".NS".  I came to know that you
> >can remove objects starting with a certain pattern; is there any way to
> >remove objects ending in a certain pattern?
> >
> >very many thanks for your time and effort...
> >yours sincerely,
> >AKSHAY M KULKARNI
> >
> >       [[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.
>
> --
> Sent from my phone. Please excuse my brevity.
>
> ______________________________________________
> [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: subsetting ls() as per class...

R help mailing list-2
In reply to this post by akshay kulkarni
> objClasses <- unlist(eapply(.GlobalEnv, function(x)class(x)[1]))
> head(objClasses)
            f             E
   "function" "environment"
           df             h
     "tbl_df"    "function"
       myData             L
       "list"        "list"
> names(objClasses)[objClasses=="tbl_df"]
[1] "df"  "out"


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Fri, Jul 27, 2018 at 10:58 PM, akshay kulkarni <[hidden email]>
wrote:

> dear memebers,
>                                I am using R in AWS linux instance for my
> research. I want to remove certain objects from the global environment  to
> reduce my EBS cost..for example, I want to remove all objects of class
> "xts", "zoo". Is there any way to automate this, instead of removing the
> objects one by one?
>
> Basically, I want to subset  ls() according to class, and then remove that
> subset by using rm function.
>
> I got to know about mget in SO, but that is not working in my case....
>
> Also, all the above objects end with ".NS".  I came to know that you can
> remove objects starting with a certain pattern; is there any way to remove
> objects ending in a certain pattern?
>
> very many thanks for your time and effort...
> yours sincerely,
> AKSHAY M KULKARNI
>
>         [[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: subsetting ls() as per class...

akshay kulkarni
In reply to this post by plangfelder
dear peter,
                     Its working....thanks a lot...

yours sincerely,
AKSHAY M KULKARNI
________________________________
From: Peter Langfelder <[hidden email]>
Sent: Saturday, July 28, 2018 11:41 AM
To: [hidden email]
Cc: r-help
Subject: Re: [R] subsetting ls() as per class...

Looking at ?rm, my solution would be something like

rm(list = grep("\\.NS$", ls(), value = TRUE))

But test it since I have not tested it.

Peter


On Fri, Jul 27, 2018 at 10:58 PM akshay kulkarni <[hidden email]> wrote:

>
> dear memebers,
>                                I am using R in AWS linux instance for my research. I want to remove certain objects from the global environment  to reduce my EBS cost..for example, I want to remove all objects of class "xts", "zoo". Is there any way to automate this, instead of removing the objects one by one?
>
> Basically, I want to subset  ls() according to class, and then remove that subset by using rm function.
>
> I got to know about mget in SO, but that is not working in my case....
>
> Also, all the above objects end with ".NS".  I came to know that you can remove objects starting with a certain pattern; is there any way to remove objects ending in a certain pattern?
>
> very many thanks for your time and effort...
> yours sincerely,
> AKSHAY M KULKARNI
>
>         [[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.