I have a dataset that looks like this:
df <- structure(list(ID = 1:10, Region1 = c("Europe", "NA",
"Asia", "NA", "Europe", "NA", "Africa", "NA", "Europe", "North America"), Region2 = c("NA", "Europe",
"NA", "NA", "NA", "Europe",
"NA", "NA", "NA", "NA"
)),
class = "data.frame", row.names = c(NA, -10L))
I want to create a new column called EuropeYN
which is either yes or no depending on whether EITHER of the region columns (region1
or region2
) include "Europe". The final data should look like this:
df <- structure(list(ID = 1:10, Region1 = c("Europe", "NA",
"Asia", "NA", "Europe", "NA", "Africa", "NA", "Europe", "North America"), Region2 = c("NA", "Europe",
"NA", "NA", "NA", "Europe",
"NA", "NA", "NA", "NA"
), EuropeYN = c("yes", "yes", "no", "no", "yes", "yes", "no", "no", "yes", "no")),
class = "data.frame", row.names = c(NA, -10L))
I know how to do this if it was just checking to see if "Europe" appears in one column, but have no idea how to do this when checking across multiple columns. This is what I would do if it was just one column:
df$EuropeYN <- ifelse(grepl("Europe",df$region1), "yes", "no")
Any ideas on the best way to approach this?...
Aucun commentaire:
Enregistrer un commentaire