[R] Loop struggle

Petr PIKAL petr.pikal at precheza.cz
Fri Mar 9 13:51:13 CET 2012


Hi

> Hi,
> 
> Ok I think I have to be more precise. Here is some example:
> 
>  The data includes tree samples (one of each species: 1, 2, 3 ) with 
> different properties that are on rows.
> 

It is difficult to reproduce your code as the data can not be easily 
transferred to R. Better to use 

dput(data)

and copy a result to your mail.

Further see in text


> > data
>   SP BAI_coef      TPH       BA          N      H R_canc_lauri  R_jakob 
   DBH
> 1  1     0.18  533.433 15.39472 0.05334331 16.723        1.616 1.540559 
19.169
> 2  2     0.10 2059.378 11.91140 0.20593779  7.376        1.197 1.076917 
 8.582
> 3  3     0.15 1034.076 14.61637 0.10340759 14.716        1.190 1.549067 
13.415
>      CL Canc_lauri  CC_lauri Canc_jakob OPT_LAI    A1    A2    A3    A4 
   A5
> 1 7.176  0.4373724 0.4373724  0.3977285    1.27 0.744 0.584 0.450 0.282 
0.186
> 2 5.648  0.9270533 0.9270533  0.7503267    1.57 0.854 0.724 0.451 0.220 
0.086
> 3 8.103  0.4601203 0.4601203  0.7795479    2.67 0.316 0.213 0.179 0.081 
0.037
>        rad1      rad2      rad3      rad4     rad5
> 1 0.1308997 0.3926991 0.6544985 0.9162979 1.178097
> 2 0.1308997 0.3926991 0.6544985 0.9162979 1.178097
> 3 0.1308997 0.3926991 0.6544985 0.9162979 1.178097
> > clump_pine<-0.98
> > clump_spruce<-0.98
> > clump_birch<-0.98
> > W_pine<-0.15
> > W_spruce<-0.15
> > W_birch<-0.15
> > n<-nrow(data)
> 
> What I am about to do is to add new columns to which new values will be 
> calculated. Here is an example on double loop that produces right 
results,
> although the error messages seem to disagree.
> 
> > for (i in 1:n) {
> + if (data$SP==1) data[1:n, "NAI"]<- data$NAI <-(1-W_pine)*data$OPT_LAI*
                                  ^^^^^^^^^^^^^^^^^ 
Why do you use double assignment to one varable?

if (data$SP==1) data$NAI <-(1-W_pine)*data$OPT_LAI*

shall be enough.

Basically you shall use ifelse instead of if for what you want to achieve 
see

?ifelse, 
?"if"

But I believe there could be better way if you do not use three different 
clump_ and W_variables but an appropriate set of variables.

Maybe someone more clever could do better with lapply but I would use loop

first prepare variable to loop over

ss<-sort(unique(data$SP))

and coefficients in correct order
W_any <- c(.98,.98,.98)
clump_any <- c(.15,.15,.15)
denom<- c(.56,.56,1)

and a new column
data$NAI<-NA

for (i in 1:length(ss)) {

rows<-which(data$SP==ss[i])
data$NAI[rows] <- (1-W[i])*data$OPT_LAI[rows]*(1/denom[i])/cl[i] 
}

Regards
Petr

> (1/0.56)/clump_pine  else 
> +      if (data$SP==2) data[1:n, "NAI"]<- data$NAI <-(1-W_spruce)*data
> $OPT_LAI*(1/0.56)/clump_spruce  else 
> +       if (data$SP==3) data[1:n, "NAI"]<- data$NAI <-(1-W_birch)*data
> $OPT_LAI*(1/1)/clump_birch 
> + } 
> Warning messages:
> 1: In if (data$SP == 1) data[1:n, "NAI"] <- data$NAI <- (1 - W_pine) * 
 :
>   the condition has length > 1 and only the first element will be used
> 2: In if (data$SP == 1) data[1:n, "NAI"] <- data$NAI <- (1 - W_pine) * 
 :
>   the condition has length > 1 and only the first element will be used
> 3: In if (data$SP == 1) data[1:n, "NAI"] <- data$NAI <- (1 - W_pine) * 
 :
>   the condition has length > 1 and only the first element will be used
> > data$NAI
> [1] 1.967019 2.431669 4.135386
> 
> Next, I am trying to do basically same thing. I wish the program would 
> loop true all samples and add five new colums (A1....A5) to which new 
> values are calculated by species specific values. Here is again the 
> problem..  The equations are replaced by number 1 or 2 or 3 just to see 
if
> loop works properly (should give values 1,2,3 because the species 1,2,3 
> are in that order). Are you able to figure out why it does not work?
> 
>  for (i in 1:n) {
> > if (data$SP==1){
> > data[1:n, "A1"]<- data$A1 <-1
> > data[1:n, "A2"]<- data$A2 <-1
> > data[1:n, "A3"]<- data$A3 <- 1
> > data[1:n, "A4"]<- data$A4 <-1
> > data[1:n, "A5"]<- data$A5 <-1}
> > else if(data$SP==2){
> > data[1:n, "A1"]<- data$A1 <-2
> > data[1:n, "A2"]<- data$A2 <-2
> > data[1:n, "A3"]<- data$A3 <-2
> > data[1:n, "A4"]<- data$A4 <-2
> > data[1:n, "A5"]<- data$A5 <-2}
> > else if(data$SP==3){
> > data[1:n, "A1"]<-data$A1 <-3
> > data[1:n, "A2"]<-data$A2 <-3
> > data[1:n, "A3"]<-data$A3 <-3
> > data[1:n, "A4"]<-data$A4 <-3
> > data[1:n, "A5"]<-data$A5 <-3}
> >
> > }
> >
> 
> -Tiff
> 
> 9. maaliskuuta 2012 10.43 Petr PIKAL <petr.pikal at precheza.cz> kirjoitti:
> Hi
> 
> you are coming from different language paradigm?
> 
> Although you did not provide your data I presume you have data frame
> called data with columns SP, A1-A5
> 
> Your construction
> 
> data[1:n, "A1"]<- data$A1 <-1

> seems to me rather strange and basically your cycle shall do
> 
> data$A1<-data$A2<-data$A3<-data$A4<-data$A5<-data$SP
> 
> or if your columns were suitably located
> 
> data[,1:5] <- data$SP
> 
> If you want something else you shall provide some working example.
> 
> Regards
> Petr
> 
> > Hi,
> >
> > I cannot get rid of  this error message:
> >
> > "Warning messages:
> > 1: In if (data$SP == 1) { :
> >   the condition has length > 1 and only the first element will be used
> > 2: In if (data$SP == 1) { :
> >   the condition has length > 1 and only the first element will be used
> > 3: In if (data$SP == 1) { :
> >   the condition has length > 1 and only the first element will be used
> > > data$A5
> > [1] 1 1 1"
> >
> > The loop seems to be stuck within the first loop. I have data (in .csv
> > format) witch contains one example of each SP within the data, so the
> > output should look like "123" instead of  "111". This example is
> simplified
> > version just to give you the idea of the problem. What is wrong within
> my
> > code? I have tried everything imaginable and cannot figure it out.
> >
> > for (i in 1:n) {
> > if (data$SP==1){
> > data[1:n, "A1"]<- data$A1 <-1
> > data[1:n, "A2"]<- data$A2 <-1
> > data[1:n, "A3"]<- data$A3 <-1
> > data[1:n, "A4"]<- data$A4 <-1
> > data[1:n, "A5"]<- data$A5 <-1}
> > else if(data$SP==2){
> > data[1:n, "A1"]<- data$A1 <-2
> > data[1:n, "A2"]<- data$A2 <-2
> > data[1:n, "A3"]<- data$A3 <-2
> > data[1:n, "A4"]<- data$A4 <-2
> > data[1:n, "A5"]<- data$A5 <-2}
> > else if(data$SP==3){
> > data[1:n, "A1"]<-data$A1 <-3
> > data[1:n, "A2"]<-data$A2 <-3
> > data[1:n, "A3"]<-data$A3 <-3
> > data[1:n, "A4"]<-data$A4 <-3
> > data[1:n, "A5"]<-data$A5 <-3}
> >
> > }
> >
> > -Tiff
> >
> >    [[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