I would like to create a dummy if an action happens in a capital city and my dataset contains 34 countries in it. Also, some times can happen that the word is within a larger string (e.g. "Berlin, Germany, DE").
Let's say the column looks as follows:
Location
1 Manchester
2 Berlin
3 Paris, France
4 Kansas
I would like the Dummy to produce the following output:
Location Capital_Dummy
1 Manchester 0
2 Berlin 1
3 Paris, France 1
4 Kansas 0
Any idea about how I could do that?
I have tried the following, which I hoped that would at least work for the cases in which only the name of the capital appears in the column but had no success even with that (making it shorter for the sake of simplicity):
capital <- c(“Madrid”, “Berlin”, “Paris”, “Prague”, “Bratislava”)
capital_dummy[df$event_location == capital] <- 1
The solution to the question, proposed by David Arenburg:
capital <- c("Madrid", "Berlin", "Paris", "Prague", "Bratislava")
capital_dummy <- grepl(paste(capital, collapse = "|"), df$Location) + 0L