[R] cut variable within a loop

Michael Dewey lists at dewey.myzen.co.uk
Tue Aug 11 17:25:04 CEST 2015


Dear Janka
If you supply a single number to the breaks parameter of cut I think it 
is the number of intervals.

On 11/08/2015 13:57, Janka Vanschoenwinkel wrote:
> 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 at 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 at 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 at 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.
>>>
>>
>>
>
>

-- 
Michael
http://www.dewey.myzen.co.uk/home.html



More information about the R-help mailing list