[R] nested loop for

Dennis Murphy djmuser at gmail.com
Thu Jul 21 23:18:37 CEST 2011


Hi:

I *think* this is what you're after, but I get dizzy trying to read
convoluted loops. Try this instead;

# Function to find the total SBA2 for all dbh2 larger than the given one:
foo <- function(d) {
    d <- d[order(d$dbh2), ]
    d <- transform(d, BAL2 = sum(SBA2) - cumsum(SBA2))
    d
  }

library('plyr')
ddply(kk, .(Plot), foo)
   Pma Plot     dbh2      SBA2      BAL2
1   29    1 185.8568  9.055821 91.530165
2   33    1 186.4623 10.762347 80.767818
3    1    1 192.8324 10.741988 70.025830
4   17    1 196.2093  9.484601 60.541229
5   21    1 204.0971 11.389817 49.151412
6   13    1 204.5070  9.644655 39.506756
7    9    1 205.3079 11.014892 28.491864
8   25    1 206.5908 10.041878 18.449986
9    5    1 206.8110  8.602678  9.847307
10  37    1 211.1735  9.847307  0.000000
...

# If you want the groupwise sum in the output, use the following
version instead:
foo <- function(d) {
    d <- d[order(d$dbh2), ]
    d <- transform(d, BAL2 = sum(SBA2) - cumsum(SBA2),
                                 aa = sum(SBA2))
    d
  }

HTH,
Dennis

On Thu, Jul 21, 2011 at 4:13 AM, paloma ruiz benito
<paloma_rb2 at hotmail.com> wrote:
>
> Hi everyone,
>
> I have been working some days in a nested loop in R but I can't find the solution.
>
> I have a data.frame with an unique ID for individuals and unique ID for different stands, for each indiviadual I have a dbh record and a SBA (stand basal area) field.
>
> Pma<-rep (1:40)
> P<-seq(1,4, 1)
> Plot<-rep(P,10)
> dbh2<-rnorm(40, mean=200, sd=5)
> SBA2<-rnorm(40, mean=10, sd=1)
>
> As I want to calculate the basal area of larger trees in each stand (i.e., the compare tree to tree the dbh and for each individual sum the stand basal area of larger trees)
>
> BAL2<-rep(0,length(Pma))
> aa<-rep(0,length(Pma))
>
> Now I have programed a for loop to do this calculation, and one plot it works quite well:
>
> kk<-data.frame(Pma, Plot, dbh2, SBA2, BAL2, aa)
> kkk<-kk[kk$Plot=="1",]
>
>
> The loop:
>
>        for(j in 1:length(kkk$Pma)){
>            for(i in 1:length(kkk$Pma)){
>                if(kkk$dbh2[j]<kkk$dbh2[i])
>                kkk$aa[i]<-kkk$SBA2[i]
>                else temp_data$aa[i]<-0
>                kkk$BAL2[j]<-sum(kkk$aa)
>    }
>    }
>
> But, I have tried a million of forms to calculate this for each stand... and no one looks fine. The closest code that I have got is:
>
> for(k in 1:length(kk)){
>    temp_data<-kk[kk$Plot==Plot[k],]
>
>        for(j in 1:length(temp_data$Pma)){
>    #I have selected the individuals to calculate the BAL in each plot
>            for(i in 1:length(temp_data$Pma)){
>                if(temp_data$dbh2[j]<temp_data$dbh2[i])
>                temp_data$aa[i]<-temp_data$SBA2[i]
>                else temp_data$aa[i]<-0
>                temp_data$BAL2[j]<-sum(temp_data$aa)
>
>
>    }
>
>    ###Some suggestion to "save" the temporal data of each stand and group it together
>    }
>
> }
>
> Any advise or suggestion will be very welcome,
>
> Thanks in advance,
>
> Paloma
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list