[R] indexing in data frames

William Dunlap wdunlap at tibco.com
Fri Aug 10 01:36:51 CEST 2012


And if you are extremely concerned with speed, do
not compute a$b and a$c in every iteration of the loop.
E.g., change
   lapply(seq_along(a$c),function(x) a$b[x]-a$c[[x]])
to something like
   with(a, lapply(seq_along(c), function(x)b[x] - c[[x]]))

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of R. Michael Weylandt
> Sent: Thursday, August 09, 2012 4:22 PM
> To: arun
> Cc: R help; jimi adams
> Subject: Re: [R] indexing in data frames
> 
> On Thu, Aug 9, 2012 at 5:30 PM, arun <smartpink111 at yahoo.com> wrote:
> >
> >  lapply(1:length(a$c),function(x) a$b[x]-a$c[[x]])
> 
> Arun,
> 
> I've seen you use this idiom a few times lately and I'd just like to note that
> 
> seq_along()
> 
> is an (underutilized) primitive and a safer and faster alternative
> (avoiding the pathological length(x) = 0 case).
> 
> Cheers,
> Michael
> 
> ______________________________________________
> 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