[R] stupid R tricks
dwinsemius at comcast.net
Sun Nov 7 20:06:35 CET 2010
On Nov 7, 2010, at 12:25 PM, David Winsemius wrote:
> On Nov 7, 2010, at 11:40 AM, Carl Witthoft wrote:
>> Hi all,
>> Just thought I'd post this (maybe) helpful tool I wrote. For
>> people like me who are bad at keeping a clean environment, it's a
>> #simple command to get only one type of object in current environment
>> if (type=='function') type <-'closure'
> As a fellow messy-enviromnetalist that was useful. Here's a similar
> function that returns a vector of object names belonging to a
> particular (single) class:
> > getclass <- function(cls) ls(envir=.GlobalEnv)[
> sapply(ls(envir=.GlobalEnv), function(x)
> class(eval(parse(text=x))) ) == cls ]
Here is a version that substitutes get(...) for eval(parse(text= ...)
making it a bit less subject to "fortune hunters" and removes the
limitation to one-class objects:
getclass <- function(cls="data.frame") ls(envir=.GlobalEnv)[
function(y) cls %in% y) ]
> > getclass("data.frame")
>  "d" "df" "set1HLI"
> > getclass("function")
>  "capture.ps" "getclass" "getsizes" "getweek"
>  "maintainer" "myfunction" "rd.txt" "rhelpSearch"
> It is not designed to identify multi-class objects, so it would miss
> a class of "lm" if there were 2 members in a class vector.
>> R-help at r-project.org mailing list
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> David Winsemius, MD
> West Hartford, CT
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
West Hartford, CT
More information about the R-help