Re: [R] lapply not reading arguments from the correct environment

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Fri, 18 May 2007 13:42:06 -0400

On 5/18/07, jiho <jo.irisson_at_gmail.com> wrote:
> On 2007-May-18 , at 18:21 , Gabor Grothendieck wrote:
> > In particular, we can use "[" directly instead of subset. This is the
> > same as your function except for the line marked ### :
> >
> > myfun2 <- function() {
> > foo = data.frame(1:10,10:1)
> > foos = list(foo)
> > fooCollumn=2
> > cFoo = lapply(foos, "[", fooCollumn) ###
> > return(cFoo)
> > }
> > myfun2() # test
> >
> > On 5/18/07, Prof Brian Ripley <ripley_at_stats.ox.ac.uk> wrote:
> >> You need to study carefully what the semantics of 'subset' are. The
> >> function body of myfun is not in the evaluation environment. (The
> >> issue
> >> is 'subset', not 'lapply': select is an *expression* and not a
> >> value.)
> >>
> >> Hint: using subset() programmatically is almost always a mistake.
> >> R's
> >> subsetting function is '[': subset is a convenience wrapper.
>
> Thank you very much. Indeed it is much better this way. I got used to
> subset for data.frames because [ does not work with negative named
> arguments while select does. E.g.:
> x[,-c("name1","name2")]
> does not work while
> subset(x,select=-c("name1","name2"))
> works (it eliminates columns named name1 and name 2 from x). But I
> guess in most cases an other syntax can achieve the same thing with
> [, like:
> x[,-which(names(x)%in%c("name1","name2"))]
> it's just a little less clear.

which is not needed. Using builtin CO2:

      CO2[ ! names(CO2) %in% c("Type", "conc" ]

or

      CO2[ setdiff(names(CO2), c("Type", "conc")) ]



R-help_at_stat.math.ethz.ch 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. Received on Fri 18 May 2007 - 17:47:12 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 18 May 2007 - 19:31:55 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.