Though, from my limited understanding, the 'apply' family of functions are actually just loops. Please correct me if I'm wrong. So, while more readable (which is important), they're not necessarily more efficient than explicit 'for' loops.
allie On 9/2/2009 3:13 AM, Phil Spector wrote: > Here's one way (assuming your data frame is named dat): > > with(dat, > data.frame(a,t(sapply(a,function(x){ > apply(dat[a - x >= -5 & a - x <= > 0,c('b','c')],2,sum)})))) > > > - Phil Spector > Statistical Computing Facility > Department of Statistics > UC Berkeley > spec...@stat.berkeley.edu > > > > On Tue, 1 Sep 2009, dolar wrote: > >> >> Would like some tips on how to avoid loops as I know they are slow in R >> >> i've got a data frame : >> >> a b c >> 1 5 2 >> 4 6 9 >> 5 2 3 >> 8 3 2 >> >> What i'd like is to sum for each value of a, the sum of b and the sum >> of c >> where a equal to or less than (with a distance of 5) >> >> i.e. for row three >> we have a=5 >> i'd like to sum up b and sum up c with the above rule >> since 5, 4 and 1 are less than (within a distance of 5) or equal to >> 5, then >> we should get the following result: >> >> a b c >> 5 13 14 >> >> the overall result should be >> a b c >> 1 5 2 >> 4 11 11 >> 5 13 14 >> 8 11 14 >> >> how can i do this without a loop? >> -- >> View this message in context: >> http://www.nabble.com/Avoiding-loops-tp25251376p25251376.html >> Sent from the R help mailing list archive at Nabble.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. >> > > ______________________________________________ > 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-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.