I have a dataset with multiple names in a column separated by commas. I want to create new variables based on whether a certain observation contains a certain name. The problem is that the names have leading and trailing white spaces and excessive spaces in the middle of a name so I want to remove them first with str_trim and str_squish. Here's an example:
library(tidyverse)
favs <- c("The Departed, The Green Mile,IT ,Spirit,The Irishman",
" Titanic, The Shawshank Redemption ",
" The Godfather ,Pulp Fiction")
mov_data <- data.frame(favs)
mov_data <- mov_data %>% mutate(movies = str_split(favs,",")) # Creates a column of vectors. This is how i want the end result after fixing the names to look like.
mov_data %>% unnest() %>%
mutate(movies = str_squish(str_trim(movies))) # fixes the names
The code above fixes the names but when I try to nest it back into a vector using nest() , the names get nested into a tibble and not into the vectors they originally came from. How do I nest the rows back into a column of vectors?