Reputation: 19
I got a data frame that has a column with names separated by commas, I want to create a vector that includes each name independently inside but my solution didn't work. Need help with it.
library(tidyverse)
cast <- netflix_titles$cast
names <- c()
for(i in cast){
splitted <- strsplit(i, ",")
for(act in splitted){
append(names, act)
}
}
rows are in this format
"Jesse Eisenberg, Woody Harrelson, Emma Stone, Abigail Breslin, Amber Heard, Bill Murray, Derek Graf"
Upvotes: 0
Views: 43
Reputation: 5254
You can get a vector of names with unlist(strsplit())
. strsplit
itself returns a list which you can turn into an atomic vector with unlist
.
unlist(strsplit("Jesse Eisenberg, Woody Harrelson, Emma Stone, Abigail Breslin, Amber Heard, Bill Murray, Derek Graf", ", "))
#> [1] "Jesse Eisenberg" "Woody Harrelson" "Emma Stone" "Abigail Breslin"
#> [5] "Amber Heard" "Bill Murray" "Derek Graf"
Hence, you can completely remove the for
loop if you add unlist()
.
You can even do it for the whole column in the data frame:
df <- data.frame(cast = c(
"Jesse Eisenberg, Woody Harrelson, Emma Stone, Abigail Breslin, Amber Heard, Bill Murray, Derek Graf",
"Bruce Willis, Matt Damon, Brad Pitt"
))
unlist(strsplit(df$cast, ", "))
#> [1] "Jesse Eisenberg" "Woody Harrelson" "Emma Stone" "Abigail Breslin"
#> [5] "Amber Heard" "Bill Murray" "Derek Graf" "Bruce Willis"
#> [9] "Matt Damon" "Brad Pitt"
Upvotes: 1