Michael Haenlein
2011-Jul-15 21:29 UTC
[R] Convert continuous variable into discrete variable
Dear all, I have a continuous variable that can take on values between 0 and 100, for example: x<-runif(100,0,100) I also have a second variable that defines a series of thresholds, for example: y<-c(3, 4.5, 6, 8) I would like to convert my continuous variable into a discrete one using the threshold variables: If x is between 0 and 3 the discrete variable should be 1 If x is between 3 and 4.5 the discrete variable should be 2 If x is between 4.5 and 6 the discrete variable should be 3 If x is between 6 and 8 the discrete variable should be 4 If x is larger than 8 the discrete variable should be 5 Is there a straightforward way of doing this (besides working with several if statements in a row)? Thanks, Michael [[alternative HTML version deleted]]
Dennis Murphy
2011-Jul-15 22:13 UTC
[R] Convert continuous variable into discrete variable
Hi: x<-runif(100,0,100) u <- cut(x, breaks = c(0, 3, 4.5, 6, 8, Inf), labels = c(1:5)) Based on the x I obtained,> table(u)u 1 2 3 4 5 3 2 1 2 92 cut() or findInterval() are the two basic functions for discretizing a numeric variable. HTH, Dennis On Fri, Jul 15, 2011 at 2:29 PM, Michael Haenlein <haenlein at escpeurope.eu> wrote:> Dear all, > > I have a continuous variable that can take on values between 0 and 100, for > example: x<-runif(100,0,100) > > I also have a second variable that defines a series of thresholds, for > example: y<-c(3, 4.5, 6, 8) > > I would like to convert my continuous variable into a discrete one using the > threshold variables: > > If x is between 0 and 3 the discrete variable should be 1 > If x is between 3 and 4.5 the discrete variable should be 2 > If x is between 4.5 and 6 the discrete variable should be 3 > If x is between 6 and 8 the discrete variable should be 4 > If x is larger than 8 the discrete variable should be 5 > > Is there a straightforward way of doing this (besides working with several > if statements in a row)? > > Thanks, > > Michael > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Frank Harrell
2013-Apr-29 11:21 UTC
[R] Convert continuous variable into discrete variable
It is important to check for lack of fit of the categorized variable. One way to do this is to test for the additional predictive ability of the original continuous variable after adjusting for its categorized version. It is very uncommon for a categorized continuous variable to fit well, because its assumed discontinuities seldom exist in nature and most relationships are not piecewise flat. Frank levanovd wrote> Or even simpler (no need to specify labels): > > x<-runif(100,0,100) > u <- cut(x, breaks = c(0, 3, 4.5, 6, 8, Inf), labels = FALSE)----- Frank Harrell Department of Biostatistics, Vanderbilt University -- View this message in context: http://r.789695.n4.nabble.com/Convert-continuous-variable-into-discrete-variable-tp3671032p4665699.html Sent from the R help mailing list archive at Nabble.com.