samedi 27 juin 2020

Using if else with R for three conditions [duplicate]

Apologies for the basic question, I am still a beginner with R!

I have a variable like this: ethnicity <- c(1A, 1B, 1C, 2A, 2B, 3A, 3C, 99, 98)

I want to create a new dichotomous variable, with one exception. I want to recode the values beginning with a 1 as 1, and the values 2A, 2B, 3A, and 3C as 0. 99 and 98 I want to be recoded into 99.

I have this.

testdata %>%
        mutate(ethnicitydi = ifelse(ethnicity %in% c("1A","1B","1C"), 1, 0))

This gives me a new variable with two levels, 1 and 0. The only problem is that I also want 99 and 98 recoded into 99.

Aucun commentaire:

Enregistrer un commentaire