[R] Creating Binary Outcomes from a continuous variable

Douglas Bates bates at stat.wisc.edu
Thu Jul 8 16:21:58 CEST 2004


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 Schwartz
For 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  :-)




More information about the R-help mailing list