Doran, Harold
2004-Jul-07 12:57 UTC
[R] Creating Binary Outcomes from a continuous variable
Dear List: I have searched the archives and my R books and cannot find a method to transform a continuous variable into a binary variable. For example, I have test score data along a continuous scale. I want to create a new variable in my dataset that is 1=above a cutpoint (or passed the test) and 0=otherwise. My instinct tells me that this will require a combination of the transform command along with a conditional selection. Any help is much appreciated. Thanks, Harold [[alternative HTML version deleted]]
Roger Bivand
2004-Jul-07 13:01 UTC
[R] Creating Binary Outcomes from a continuous variable
On Wed, 7 Jul 2004, Doran, Harold wrote:> Dear List: >?cut> > > I have searched the archives and my R books and cannot find a method to > transform a continuous variable into a binary variable. For example, I > have test score data along a continuous scale. I want to create a new > variable in my dataset that is 1=above a cutpoint (or passed the test) > and 0=otherwise. > > > > My instinct tells me that this will require a combination of the > transform command along with a conditional selection. Any help is much > appreciated. > > > > Thanks, > > Harold > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Breiviksveien 40, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93 e-mail: Roger.Bivand at nhh.no
Marc Schwartz
2004-Jul-07 13:03 UTC
[R] Creating Binary Outcomes from a continuous variable
On Wed, 2004-07-07 at 07:57, Doran, Harold wrote:> Dear List: > > I have searched the archives and my R books and cannot find a method to > transform a continuous variable into a binary variable. For example, I > have test score data along a continuous scale. I want to create a new > variable in my dataset that is 1=above a cutpoint (or passed the test) > and 0=otherwise.> My instinct tells me that this will require a combination of the > transform command along with a conditional selection. Any help is much > appreciated.Example:> a <- rnorm(20) > b <- ifelse(a < 0, 0, 1)> a[1] -1.0735800 -0.6788456 1.9979801 -0.4026760 0.1781791 -1.1540434 [7] -1.0842728 1.6042602 -0.7950492 -0.1194323 0.4450296 1.9269333 [13] -0.4456181 -0.8374677 -1.1898772 1.7353067 1.8619422 -0.1679996 [19] -0.2656138 -1.5529884> b[1] 0 0 1 0 1 0 0 1 0 0 1 1 0 0 0 1 1 0 0 0 HTH, Marc Schwartz
Douglas Bates
2004-Jul-08 14:21 UTC
[R] Creating Binary Outcomes from a continuous variable
Marc Schwartz wrote:> On Wed, 2004-07-07 at 07:57, Doran, Harold wrote: > >>Dear List: >> >>I have searched the archives and my R books and cannot find a method to >>transform a continuous variable into a binary variable. For example, I >>have test score data along a continuous scale. I want to create a new >>variable in my dataset that is 1=above a cutpoint (or passed the test) >>and 0=otherwise. > > >>My instinct tells me that this will require a combination of the >>transform command along with a conditional selection. Any help is much >>appreciated. > > > Example: > > >>a <- rnorm(20) >>b <- ifelse(a < 0, 0, 1) > > >>a > > [1] -1.0735800 -0.6788456 1.9979801 -0.4026760 0.1781791 -1.1540434 > [7] -1.0842728 1.6042602 -0.7950492 -0.1194323 0.4450296 1.9269333 > [13] -0.4456181 -0.8374677 -1.1898772 1.7353067 1.8619422 -0.1679996 > [19] -0.2656138 -1.5529884 > >>b > > [1] 0 0 1 0 1 0 0 1 0 0 1 1 0 0 0 1 1 0 0 0 > > HTH, > > Marc SchwartzFor your application Harold I would consider converting the response to a factor while dichotomizing it so that summary will give a meaningful table b <- factor(ifelse(a < 0, "Neg", "Pos")) BTW, there are many examples like this in the notes for the short course that you took last summer :-)