[R] Creating Binary Outcomes from a continuous variable

Marc Schwartz MSchwartz at MedAnalytics.com
Wed Jul 7 15:03:06 CEST 2004


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




More information about the R-help mailing list