Home > Blockchain >  Convert binary variable from having int values to string values in r
Convert binary variable from having int values to string values in r

Time:10-11

My dependent variable looks like this:

$ dpnm : int 1 1 0 0 0 0 0 0 0 0 ...

And I want to convert the 1s int "Yes" and the 0s into "No"

I have tried:

default$dpnm <- revalue(default$dpnm , c(1 = "Yes"))
default$dpnm <- revalue(default$dpnm , c(0 = "No"))

CodePudding user response:

In base R you can use boolean indexing:

default$dpnm[default$dpnm==0]  <- "No"
default$dpnm[default$dpnm==1]  <- "Yes"

Depending on what you are doing, it may make more sense to convert it to an ordered factor with labels:

default$dpnm  <- factor(
    default$dpnm, 
    levels = c(0,1), 
    labels = c("No", "Yes"),
    ordered = TRUE
)
  • Related