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.
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 spector at 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 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. >
> Would like some tips on how to avoid loops as I know they are slow in RThey are not slow. They are slower than vectorised equivalents, but not slower than apply and friends. Hadley -- http://had.co.nz/
On Tue, 1 Sep 2009, dolar wrote:> > Would like some tips on how to avoid loops as I know they are slow in R >If I understand your criterion (and calling your data.frame 'dat'):> criterion <- as.matrix(dist(dat$a)) <= 5 & outer(dat$a,dat$a,">=") > criterion %*% as.matrix(dat[, c("b","c")])b c 1 5 2 2 11 11 3 13 14 4 11 14 HTH, Chuck> 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 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901