Oscar Silveiro
Oscar Silveiro

Reputation: 35

Create a new variable

I have the following data:

image

I want to create a variable "TITLE" from "NAME" with the values ​​MASTER, MISS, MR, MRS AND OTHER. MISS, sometimes is like MLLE, and MRS sometimes appears as Ms or MME, using dplyr package.

I tried this:

Title_Master <- titanic2 %>% 
  filter(str_detect(Name, "Master") & Sex == "male") %>%
  mutate(Title = "Master")

Title_Miss <- titanic2 %>%
  filter((str_detect(Name, "Miss") | str_detect(Name, "Mmlle")) & Sex == 
  "female") %>%   
  mutate(Title = "Miss")

Title_Mr <-  titanic2 %>% 
  filter(str_detect(Name, "Mr") & Sex == "male") %>%
  mutate(Title = "Mr")

Title_Mrs <- titanic2 %>% 
  filter((str_detect(Name, "Mrs") | str_detect(Name, "Ms") |  
  str_detect(Name, "Mme")) & Sex == "female") %>%             
  mutate(Title = "Mrs")

T_Title <- rbind(Title_Master, Title_Miss, Title_Mr, Title_Mrs)

But I'm not sure it's the best way. And I don't know how to create the value "OTHER".

Upvotes: 1

Views: 174

Answers (1)

A. Suliman
A. Suliman

Reputation: 13125

#Always includes libraries and data set used is important for reproduciblity
library(tidyverse)
library(stringr)
#install.packages("titanic")
library(titanic)
titanic2 <- titanic::titanic_test

titanic2 %>% mutate(Title = case_when(str_detect(Name, "Master") & Sex == "male" ~ "Master", 
                               str_detect(Name, "Miss|Mmlle") & Sex == "female" ~ "Miss",
                               str_detect(Name, "Mr") & Sex == "male" ~ "Mr",
                               str_detect(Name, "Mrs|Ms|Mme") & Sex == "female" ~ "Mrs",
                               TRUE ~ "OTHER")) %>% group_by(Sex, Title) %>% summarise(N=n())

  # A tibble: 6 x 3
  # Groups:   Sex [?]
   Sex    Title     N
   <chr>  <chr>  <int>
1 female Miss      78
2 female Mrs       73
3 female OTHER      1
4 male   Master    21
5 male   Mr       240
6 male   OTHER      5

Upvotes: 1

Related Questions