vendredi 12 février 2021

r: ifelse() based on whether last digit of as.character is odd or even

I have a list of (fake) social security numbers here

> df
          cpr
1 120234-1353
2 031254-4444

I want to create df$sex like a ifelse()-solution, so that if the last digit of the character string is odd, then M else F. How can this be done?

Expected output

> df
          cpr    sex
1 120234-1353      M
2 031254-4444      F

Data

df <- structure(list(cpr = c("120234-1353", "031254-4444")), class = "data.frame", row.names = c(NA, 
-2L))

Aucun commentaire:

Enregistrer un commentaire