I have the following code
titanic <- titanic %>% mutate(title = ifelse(str_detect(name,"Mr.|Ms.|Mme."), "Hombre casado",
ifelse(str_detect(name, "Master."), "Hombre soltero",
ifelse(str_detect(name, "Miss."), "Mujer soltera",
ifelse(str_detect(name, "Mrs.|Mlle."), "Mujer casada", "Otro")))))
I have the following dataframe:
name <- c("Mr Sergio", "Mrs Maria")
surname <- c("Nnunci", "Gonzalez")
df <- data.frame(name, surname)
The idea of this function is to add to the title column their marital status depending if in name column there is Mr Ms or Mrs.
For example, if in the column "name" I have Mr|Ms|Mme (one of them), then in title I have to put "Hombre casado" which mean "Married man".
It's working well except for "Mrs." which mean "Married women", because when I apply this function to my dataset, Married women appear as "Hombre casado" (Married man). I think it's about the pattern I am using for detecting the types.
Output:
name surname title
Mr Sergio Nnunci Hombre Casado
Mrs Maria Gonzalez Mujer Casada
Some idea?