[R] cut variable within a loop

PIKAL Petr petr.pikal at precheza.cz
Fri Aug 14 12:01:33 CEST 2015


Hi

Didn't you by chance use plain cut and k2 was NA?

> cut(1:300, NA)
Error in cut.default(1:300, NA) : invalid number of intervals

Cheers
Petr

> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Janka
> Vanschoenwinkel
> Sent: Friday, August 14, 2015 10:16 AM
> To: Michael Dewey
> Cc: r-help at r-project.org
> Subject: Re: [R] cut variable within a loop
>
> Hey Michael,
>
> Sorry for the late reply!
>
> Thanks for your comment, but for the cut2 command, this is not the
> case. If I enter for instance
>
> Alldata$irri=cut2(irrigation,3)
>
> Then I get 2 intervals from 0-3 and from 3-100.
>
> Janka
>
> 2015-08-11 17:25 GMT+02:00 Michael Dewey <lists at dewey.myzen.co.uk>:
>
> > 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_Irri
> >>>> gation=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
> >
>
>
>
> --
>
> [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]]
>
> ______________________________________________
> 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.

________________________________
Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.


More information about the R-help mailing list