[R] Help with the Cut Function

David Winsemius dwinsemius at comcast.net
Sun Jun 26 02:10:53 CEST 2016


> On Jun 25, 2016, at 4:24 PM, Shivi Bhatia <shivipmp82 at gmail.com> wrote:
> 
> Hi David, 
> 
> I tried as suggested however with this code:
> Age11<- cut(desc$Age, breaks = c(-Inf, 20,30,40,Inf),labels = c("Low","Mid","Top"))


Read the error message and add in another item to the 'labels' vector. Perhaps:

 c( "<= 20", "(20,30]", "(30,40], "> 40"")


> i receive an error message as below:
> 
> lengths of 'breaks' and 'labels' differ. Now as a result i have values exceeding 40 as N/A. 
> 
> On Sun, Jun 26, 2016 at 3:25 AM, David Winsemius <dwinsemius at comcast.net> wrote:
> 
> > On Jun 25, 2016, at 12:05 PM, Shivi Bhatia <shivipmp82 at gmail.com> wrote:
> >
> > Dear Team,
> >
> > Please see the code below:
> >
> > Age1<- cut(desc$Age, breaks = c(20,30,40,Inf),labels = c("Low","Mid","Top"))
> 
> Try instead:
> 
> Age1<- cut(desc$Age, breaks = c(-Inf, 20,30,40,Inf),labels = c("Low","Mid","Top"))
> 
> Do note that values that are <= 20 will be in the lowest category. (You wrote only <20 whereas values of 20 would have not been in any interval.)  The cut function also has a use.lowest argument, whose actions I don't fully understand but I usually set to to TRUE whereas is default is FALSE. I prefer the Hmisc::cut2 functions because its defaults mirror my usual interests. Like cut2, the findInterval function has closed intervals on the left.
> 
> > here i am creating three categories as mentioned from the age var from desc
> > data set.
> > All the values are set correctly however the values which are below 20 are
> > set to NA.
> > Is there anything i am doing incorrect.
> >
> > Regards, Shivi
> >
> >       [[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.
> 
> David Winsemius
> Alameda, CA, USA
> 
> 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list