I'm looking for an R function that simply recodes a quantitative variable into a number of classes according to specified break-points. Obviously I can do this using nested ifelse() commands, but I want to write it into a function where I can't pre-specify the number of classes. Is there an obvious way to do this? An example to clarify: how to convert c(0,10,5,1,9,6) to c(1,3,2,1,3,2) by specifying "breaks"=c(2.5,7.5) - or something like that. Thanks, Richard Gunton. INRA-Dijon, France
Try ?cut Greg rgunton at dijon.inra.fr wrote:> > I'm looking for an R function that simply recodes a quantitative > variable into a number of classes according to specified break-points. > Obviously I can do this using nested ifelse() commands, but I want to > write it into a function where I can't pre-specify the number of > classes. Is there an obvious way to do this? > > An example to clarify: how to convert c(0,10,5,1,9,6) to > c(1,3,2,1,3,2) by specifying "breaks"=c(2.5,7.5) - or something like > that. > > Thanks, > > Richard Gunton. > INRA-Dijon, France > > ______________________________________________ > R-help at r-project.org mailing list > 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.-- Greg Hirson ghirson at ucdavis.edu Graduate Student Agricultural and Environmental Chemistry 1106 Robert Mondavi Institute North One Shields Avenue Davis, CA 95616
Richard, More specifically, x = c(0,10,5,1,9,6) cut(x, breaks = c(-Inf, 2.5,7.5, Inf), labels = c(1, 2, 3)) #[1] 1 3 2 1 3 2 Hope that helps, Greg rgunton at dijon.inra.fr wrote:> > I'm looking for an R function that simply recodes a quantitative > variable into a number of classes according to specified break-points. > Obviously I can do this using nested ifelse() commands, but I want to > write it into a function where I can't pre-specify the number of > classes. Is there an obvious way to do this? > > An example to clarify: how to convert c(0,10,5,1,9,6) to > c(1,3,2,1,3,2) by specifying "breaks"=c(2.5,7.5) - or something like > that. > > Thanks, > > Richard Gunton. > INRA-Dijon, France > > ______________________________________________ > R-help at r-project.org mailing list > 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.-- Greg Hirson ghirson at ucdavis.edu Graduate Student Agricultural and Environmental Chemistry 1106 Robert Mondavi Institute North One Shields Avenue Davis, CA 95616