vendredi 28 septembre 2018

ifelse with list of possible string matches and no else

So normally, if I want to populate a new column data$new.col with 1s if it finds the strings "foo" or "bar" in data$strings and 0s if not, I would use something like this:

data$new.col <- ifelse(grepl("foo|bar",
  data$strings, ignore.case = T, perl = T), "1", "0")

However, I want to do the equivalent of this without an "else". I tried using a simple assignment but I must be doing something wrong because it's not working:

data$new.col[data$strings == "foo|bar"] <- "1"

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire