[R] how to speed up this for loop?
Nordlund, Dan (DSHS/RDA)
NordlDJ at dshs.wa.gov
Tue Jul 22 21:18:42 CEST 2008
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Nordlund,
> Dan (DSHS/RDA)
> Sent: Tuesday, July 22, 2008 10:31 AM
> To: r-help at r-project.org
> Subject: Re: [R] how to speed up this for loop?
>
> > -----Original Message-----
> > From: r-help-bounces at r-project.org
> > [mailto:r-help-bounces at r-project.org] On Behalf Of Rheannon
> > Sent: Tuesday, July 22, 2008 9:09 AM
> > To: r-help at r-project.org
> > Subject: Re: [R] how to speed up this for loop?
> >
> >
<<<snip>>>
> > #******This is the loop that I would like to speed up************
> >
> > #sum values 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])}}
> >
>
> Using sum for a matrix name was very confusing to my eyes
> trying to differentiate square brackets from parentheses.
> You may have even confused yourself. However, does this get
> you close to what you want?
>
> #sum values between FN and LN, excluding positive values
> for(i in 1:R){
> sum[i] <- sum(Temp[i,Temp[I,FN[i]:LN[i]]>0]) }
>
I hope it was obvious, but there is an upper case 'I' in the summation that should be lower case.
for(i in 1:R){
sum[i] <- sum(Temp[i,Temp[i,FN[i]:LN[i]]>0]) }
Dan
Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204
More information about the R-help
mailing list