[R] Avoiding loops

Charles C. Berry cberry at tajo.ucsd.edu
Wed Sep 2 17:51:40 CEST 2009


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




More information about the R-help mailing list