简体   繁体   English

将R中的二进制因子向量重新编码为虚拟变量(0,1)

[英]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

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 我不希望像as.numeric(data$outcome) [1] 1 1 1 1 2 1和2。 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. 我知道您不应该“手动”对变量进行虚拟编码以进行回归,并且我知道{psych} dummy.code() ,它返回一个矩阵。 I understand that I could use something like model.matrix() on the data.frame: 我知道我可以在data.frame上使用诸如model.matrix()之类的东西:

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

Not nice... 不是很好...

Isn't there something like dummify(data$outcomes) somewhere in R? R中是否没有类似dummify(data$outcomes)东西? 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. 因为它更加显式/不透明,即使有人为该因子添加了新的级别“ Awesome”,它仍然可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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