# Convert continuous variable into discrete variable

 Classic List Threaded
4 messages
Reply | Threaded
Open this post in threaded view
|

## 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]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: 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 <[hidden email]> 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]] > > ______________________________________________ > [hidden email] 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. > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: Convert continuous variable into discrete variable

 This post has NOT been accepted by the mailing list yet. 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)
Reply | Threaded
Open this post in threaded view
|

## Re: 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