Reputation: 3
I would like to create a dummy variables for action movies in my data set.
My code is,
imdb$action_movies <- ifelse(imdb$imdb.com_genres == "Action", 1,0)
Unfortunately when I run this code I only get movies with exclusively the Action
tag and not movies with multiple tags such as Action Adventure.
How can I make it so that my dummy variable will include movies that have the action tag and multiple other genres?
Upvotes: 0
Views: 174
Reputation: 1046
This is a relatively simple problem that regex that can solve.
Basically we want to inspect every string to see if it contains "Action". If it does we give it a 1, if it does not a 0.
We can use str_detect()
from {stringr}
to do this.
From there we throw our matches into an ifelse()
statement as you had done above.
An example of what the final column will look like this is shown below
movies <- c("Action", "Comedy, Action, Adventure", "Action, Adventure")
imdb$action_movies <- ifelse(str_detect(movies, "Action") == T, 1, 0)
Which returns
[1] 1 1 1
Upvotes: 0