[R] cut variable within a loop

Thierry Onkelinx thierry.onkelinx at inbo.be
Tue Aug 11 15:10:08 CEST 2015


You'll need to send a reproducible example of the code. We can't run the
code that you send. Hence it is hard to help you. See e.g.
http://adv-r.had.co.nz/Reproducibility.html

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

2015-08-11 14:57 GMT+02:00 Janka Vanschoenwinkel <
janka.vanschoenwinkel op uhasselt.be>:

> Hi Thierry!
>
> Thanks for your answer. I tried this, but I get this error:
>
> "Error in cut.default(x, k2) : invalid number of intervals"
>
> Which is strange because I am not specifying intervals, but the number at
> where the sample has to be cut?
>
> Greetings from Belgium! :-)
>
> 2015-08-11 14:52 GMT+02:00 Thierry Onkelinx <thierry.onkelinx op inbo.be>:
>
>> Dear Janka,
>>
>> You loop goes for 0 to 100. It should probably go from 1:99
>>
>> Best regards,
>>
>> ir. Thierry Onkelinx
>> Instituut voor natuur- en bosonderzoek / Research Institute for Nature
>> and Forest
>> team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
>> Kliniekstraat 25
>> 1070 Anderlecht
>> Belgium
>>
>> To call in the statistician after the experiment is done may be no more
>> than asking him to perform a post-mortem examination: he may be able to say
>> what the experiment died of. ~ Sir Ronald Aylmer Fisher
>> The plural of anecdote is not data. ~ Roger Brinner
>> The combination of some data and an aching desire for an answer does not
>> ensure that a reasonable answer can be extracted from a given body of data.
>> ~ John Tukey
>>
>> 2015-08-11 14:38 GMT+02:00 Janka Vanschoenwinkel <
>> janka.vanschoenwinkel op uhasselt.be>:
>>
>>> 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]]
>>>
>>> ______________________________________________
>>> R-help op r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> 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.
>>>
>>
>>
>
>
> --
>
> [image: Logo UHasselt]Mevrouw Janka Vanschoenwinkel
> *Doctoraatsbursaal - PhD *
> Milieueconomie - Environmental economics
>
> T +32(0)11 26 87 42 | GSM +32(0)476 28 21 40
>
> www.uhasselt.be/eec
>
> Universiteit Hasselt | Campus Diepenbeek
> Agoralaan Gebouw D | B-3590 Diepenbeek
> Kantoor F11
>
> Postadres: Universiteit Hasselt | Martelarenlaan 42 | B-3500 Hasselt
>
> P Please consider the environment before printing this e-mail
>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list