Chapter 11 3. Separate Passenger Names
The Name column contains both last name and first name information. We can separate this column into two columns: Last_name and First_name.
titanic <- titanic %>%
separate(
col = Name,
into = c("Last_name", "First_name"),
sep = ","
)
head(titanic %>% select(Last_name, First_name))## # A tibble: 6 x 2
## Last_name First_name
## <chr> <chr>
## 1 Braund " Mr. Owen Harris"
## 2 Cumings " Mrs. John Bradley (Florence Briggs Thayer)"
## 3 Heikkinen " Miss. Laina"
## 4 Futrelle " Mrs. Jacques Heath (Lily May Peel)"
## 5 Allen " Mr. William Henry"
## 6 Moran " Mr. James"
11.1 3.1 Remove Extra Spaces
After separating the names, the First_name column may contain extra spaces at the beginning. We can remove these using str_trim().
titanic <- titanic %>%
mutate(First_name = str_trim(First_name, side = "left"))
head(titanic %>% select(Last_name, First_name))## # A tibble: 6 x 2
## Last_name First_name
## <chr> <chr>
## 1 Braund Mr. Owen Harris
## 2 Cumings Mrs. John Bradley (Florence Briggs Thayer)
## 3 Heikkinen Miss. Laina
## 4 Futrelle Mrs. Jacques Heath (Lily May Peel)
## 5 Allen Mr. William Henry
## 6 Moran Mr. James