[R] cut variable within a loop

Janka Vanschoenwinkel janka.vanschoenwinkel at uhasselt.be
Tue Aug 11 14:38:49 CEST 2015


Dear list members,

I have a loop where I want to do several calculations for different samples
and save the results for each sample. These samples are for each loop
different. I want to use the "i" in the loop to cut the samples.

So for instance:

   - In loop 1 (i=1), I have a sample from 0-1 and a sample from 1-100.
   - In loop 2 (i=2), I have a sample from 0-2 and a sample from 2-100.
   - In loop 99 (i=99), I have a sample from 0-99 and a sample from 99-100.

I built the following function, but there is *a problem with the cut2
function* since it doesn't recognize the "i". Outside the lapply loop it
works, but not inside the loop.

Could somebody please help me with this problem? Thanks a lot!


d=data.frame(MEt_Rainfed=rep(0,100),MEp_Rainfed=rep(0,100),MEt_Irrigation=rep(0,100),MEp_Irrigation=rep(0,100))



    o<-lapply(0:100, function(i){



        Alldata$irri=cut2(Alldata$irrigation,i)

        levels(Alldata$irri)<-c("0","1")



       Alldata_Rainfed<-subset(Alldata, irri == 0)

       Alldata_Irrigation<-subset(Alldata, irri == 1)



    #calculations per sample, then store all the values per i and per
variable in a dataframe: (the calculations are not shown in this example)



     d[i, ] = c(MEt_Rainfed,MEp_Rainfed,MEt_Irrigation,MEp_Irrigation)



   })



   out<-as.data.frame(do.call(rbind, o))


-- 
P Please consider the environment before printing this e-mail

	[[alternative HTML version deleted]]



More information about the R-help mailing list