mardi 19 janvier 2021

Ifelse conditional on same strings in multiple columns

So I guess this is possible to achieve by just making a veeery long line code using mutate() and ifelse() but I want to know if there is a way of doing it without writing a tone of code.

I have data where the degree of each individual is written in a non-ordered fashion. The data looks like this:

id <- c(1, 2, 3, 4, 5, 6)
degree1 <- c("masters", "bachelors", "PhD", "bachelors", "bachelors", NA)
degree2 <- c("PhD", "masters", "bachelors", NA, NA, NA)
degree3 <- c("bachelors", NA, "masters", NA, "masters", NA)

Now I want to create a new column containing the string for the highest degree, like this

dat$highest_degree <- c("PhD", "masters", "PhD", "bachelors", "masters", NA)

How can I achieve this?

Aucun commentaire:

Enregistrer un commentaire