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

From: jiho <jo.irisson_at_gmail.com>
Date: Fri, 18 May 2007 19:01:44 +0200

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.
Thanks again.

JiHO

---
http://jo.irisson.free.fr/

______________________________________________
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:06:26 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 - 18:31:00 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.