Recode categorical variable to binary (0/1)

后端 未结 2 1882
渐次进展
渐次进展 2020-11-30 15:29

Could someone help me regarding the use of ifelse.

I have a data.frame (dat) with a categorical variable/factor called Q1 (dat$Q1

相关标签:
2条回答
  • 2020-11-30 16:13

    Use ifelse as in:

    dat$new1 <- ifelse(dat$Q1==3, 1, 0)
    
    0 讨论(0)
  • 2020-11-30 16:18

    Just:

     dat$new1 <-  0+(dat$Q1==3)  # or use as.numeric(.)
    
    0 讨论(0)
提交回复
热议问题