Re: [R] Creating Binary Outcomes from a continuous variable

From: Douglas Bates <bates_at_stat.wisc.edu>
Date: Fri 09 Jul 2004 - 00:21:58 EST

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



R-help@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 Received on Fri Jul 09 00:27:45 2004

This archive was generated by hypermail 2.1.8 : Wed 03 Nov 2004 - 22:54:46 EST