简体   繁体   中英

Convert Character to Factor R

I have the following data:

> str(hiv_data)
'data.frame':   500 obs. of  8 variables:
 $ HIV.status     : chr  "HIV negative" "HIV negative" "HIV negative" "HIV negative" ...
 $ edu.years      : int  8 7 5 8 1 6 7 NA 4 5 ...
 $ wealth.quintile: int  5 4 5 5 2 4 2 NA 4 NA ...
 $ res.type       : chr  "Urban" "Urban" "Urban" "Urban" ...
 $ marital.status : chr  "Married" "Never married before" "Never married before" "Never married before" ...
 $ province       : chr  "WCP" "FSP" "FSP" "WCP" ...
 $ employment     : chr  "yes" "no" "no" "no" ...
 $ age            : int  27 32 26 18 44 24 18 NA 24 NA ...
> 

I am performing forward selection to find a model with response HIV.status. I need to change the response to a factor with labels "Negative" and "Positive" coded as 0 and 1. I have tried doing the following:


hiv_data$HIV.status<-factor(hiv_data$HIV.status,levels=c(0,1),labels=c("HIV negative","HIV positive"))

But that results in the entire column consisting of NA values. Any help?

hiv_status <- sample(c('HIV negative', 'HIV positive'), size = 10, replace = TRUE)
hiv_status
#>  [1] "HIV positive" "HIV negative" "HIV positive" "HIV negative" "HIV negative"
#>  [6] "HIV negative" "HIV negative" "HIV negative" "HIV negative" "HIV positive"

factor(hiv_status, labels = c('HIV positive', 'HIV negative'))
#>  [1] HIV negative HIV positive HIV negative HIV positive HIV positive
#>  [6] HIV positive HIV positive HIV positive HIV positive HIV negative
#> Levels: HIV positive HIV negative

hiv_status <- +(hiv_status == 'HIV positive')
hiv_status
#>  [1] 1 0 1 0 0 0 0 0 0 1

Created on 2021-05-11 by the reprex package (v2.0.0)

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