Re: [R] how to speed up this for loop?

From: bartjoosen <>
Date: Tue, 22 Jul 2008 01:20:43 -0700 (PDT)

I'm not sure if I understand correct, but maybe something like:

apply(dat,2,function(x) sum(x[x<0]))

Please do read the posting guide and post a small, working example!!!


Rheannon wrote:
> Could anyone tell me a better way to achieve the output of this for loop?
> It seems to run quite slow. I'm sure there must be a more consise way to
> sum from FN to LN, excluding positive values, for each row.
> #sum between FN and LN, excluding positive values
> for(i in 1:R){
> for(j in FN[i]:LN[i]){
> if(Temp[i,j]<0)
> sum[i] <- sum[i] + sum(Temp[i,j])}}
> Cheers,
> R

View this message in context:
Sent from the R help mailing list archive at

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Tue 22 Jul 2008 - 08:24:15 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 22 Jul 2008 - 09:32:03 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive