简体   繁体   中英

Recode a vector of binary factors in R as a dummy variable (0, 1)

I have a vector (really a column of a data frame) that looks like this:

data$outcome
[1] Good Good Good Good Poor
Levels: Good Poor

Here is the str on it:

str(data$outcome)
 Factor w/ 2 levels "Good","Poor": 1 1 1 1 2

I don't want 1's and 2's as in as.numeric(data$outcome) [1] 1 1 1 1 2

I know you are not supposed to dummy-code the variables "manually" for regression, and I know about {psych} dummy.code() , which returns a matrix. I understand that I could use something like model.matrix() on the data.frame:

data$outcome <- model.matrix(lm(s100b ~ outcome, data))[,2]

Not nice...

Isn't there something like dummify(data$outcomes) somewhere in R? Please refrain from easy jokes...

I slightly prefer

data$isGood <- as.numeric(data$outcome == 'Good')

because it is a bit more explicit / less opaque, and would still work even if someone added a new level 'Awesome' to the factor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM