## 1.12 Dichotomizing a measure into 2 categories

Dichotomous variables tend to be binary indicator variables where a code of 1 is the level you’re interested in.

For example, in this study gender is coded as 2=Female and 1=Male. (This data was collected in the ’70s, and so only two genders were provided as options). We want to convert this 1=Female and 0=Male.

depress$sex <- depress$sex -1
table(depress$sex) ## ## 0 1 ## 111 183 0/1 binary coding is mandatory for many analyses. One simple reason is that now you can calculate the mean and interpret it as a proportion. mean(depress$sex)
## [1] 0.622449

62% of individuals in this data set are female.

Sometimes the data is recorded as 1/2 (Yes/No), so just subtracting from 1 doesn’t create a positive indicator of the variable. For example, drink=1 if they are a regular drinker, and drink=2 if they are not. We want not drinking to be coded as 0, not 2.

table(depress$drink) ## ## 1 2 ## 234 60 The ifelse() function says that if depress$DRINK has a value equal to 2 ==2, then change the value to 0. Otherwise leave it alone.

depress$drink <- ifelse(depress$drink==2, 0, depress$drink) table(depress$drink)
##
##   0   1
##  60 234