Re: [R] stupid R tricks
On 11/07/2010 08:06 PM, David Winsemius wrote: 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 time-saver. #simple command to get only one type of object in current environment lstype<-function(type='closure'){ inlist<-ls(.GlobalEnv) if (type=='function') type <-'closure' typelist<-sapply(sapply(inlist,get),typeof) return(names(typelist[typelist==type])) } 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)[ sapply( sapply(ls(envir=.GlobalEnv), function(x) class(get(x)) ), function(y) cls %in% y) ] Hello, A simpler, less verbose version: getclass <- function(cls="data.frame") Filter( function( b ) any( cls %in% class( get( b ) ) ), ls( envir=.GlobalEnv ) ) Best regards, Carlos J. Gil Bellosta http://www.datanalytics.com __ R-help@r-project.org mailing list 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.
Re: [R] stupid R tricks
See 'mode' argument in ls.str function. On Sun, Nov 7, 2010 at 2:40 PM, 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 time-saver. > > #simple command to get only one type of object in current environment > lstype<-function(type='closure'){ >inlist<-ls(.GlobalEnv) >if (type=='function') type <-'closure' >typelist<-sapply(sapply(inlist,get),typeof) >return(names(typelist[typelist==type])) > } > > > > Carl > > __ > R-help@r-project.org mailing list > 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. > -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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.
Re: [R] stupid R tricks
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 time-saver. #simple command to get only one type of object in current environment lstype<-function(type='closure'){ inlist<-ls(.GlobalEnv) if (type=='function') type <-'closure' typelist<-sapply(sapply(inlist,get),typeof) return(names(typelist[typelist==type])) } 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)[ sapply( sapply(ls(envir=.GlobalEnv), function(x) class(get(x)) ), function(y) cls %in% y)] -- David. > getclass("data.frame") [1] "d" "df" "set1HLI" > getclass("function") [1] "capture.ps" "getclass""getsizes""getweek" "is.defined" [6] "maintainer" "myfunction" "rd.txt" "rhelpSearch" "write.clip" 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. Carl __ R-help@r-project.org mailing list 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. David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list 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. David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list 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.
Re: [R] stupid R tricks
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 time-saver. #simple command to get only one type of object in current environment lstype<-function(type='closure'){ inlist<-ls(.GlobalEnv) if (type=='function') type <-'closure' typelist<-sapply(sapply(inlist,get),typeof) return(names(typelist[typelist==type])) } 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 ] > getclass("data.frame") [1] "d" "df" "set1HLI" > getclass("function") [1] "capture.ps" "getclass""getsizes""getweek" "is.defined" [6] "maintainer" "myfunction" "rd.txt" "rhelpSearch" "write.clip" 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. Carl __ R-help@r-project.org mailing list 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. David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list 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.
[R] stupid R tricks
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 time-saver. #simple command to get only one type of object in current environment lstype<-function(type='closure'){ inlist<-ls(.GlobalEnv) if (type=='function') type <-'closure' typelist<-sapply(sapply(inlist,get),typeof) return(names(typelist[typelist==type])) } Carl __ R-help@r-project.org mailing list 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.