[R] weird apply() behavior

William Dunlap wdunlap at tibco.com
Tue Aug 30 05:53:38 CEST 2011


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Heikki Kaskelma
> Sent: Monday, August 29, 2011 6:13 PM
> To: r-help at stat.math.ethz.ch
> Subject: Re: [R] weird apply() behavior
> 
> "array chip":
> >Hi, I had a weird results from using apply().
> 
> Consider:
> 
> y<-data.frame(list(a=c(1,NA), b=c('2k','0')))
> do.call(args=y,
>             Vectorize(v=c("a","b"),
>                              function(a,b) if(is.na(z<-b=="2k" & a==1)) 0
> else z+0))

I find the following easier to understand, but there
is no accounting for taste.  It is faster, by a factor
of c. 50 when y is blown up to have 10000 rows.
  > with(y, as.integer(!is.na(b) & b=="2k" & !is.na(a) & a==1))
  [1] 1 0

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> 
> Heikki Kaskelma
> Munkkiniemi
> 
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list