1

I have two columns in a data.frame x called "preferred_cat" and "var_nm". I want to create a third column called "preferred_tag" with value either 1 or 0.

1- if preferred_cat(for ex qq) is subset of var_nm (for ex jdsajqq) or
0- if preferred_cat(for ex qq) is not subset of var_nm (for ex qdsajq)

x <- x %>% mutate(preferred_tag=ifelse(grepl(preferred_cat,var_nm,fixed=TRUE),1,0))

However I am getting this warning:

Warning message:
In grepl(preferred_cat, var_nm, fixed = TRUE) :
  argument 'pattern' has length > 1 and only the first element will be used

What is does this warning mean, and how can I avoid it?

cmaher
  • 5,100
  • 1
  • 22
  • 34
itthrill
  • 1,241
  • 2
  • 17
  • 36
  • 3
    `grepl(pattern=c("a","b"), c("a","b","a","b"))` might be illuminating. The `pattern=` argument only takes a single regular expression string - the first one is used (`"a"`) if more than 1 is given. This has nothing to do with either `ifelse` or `dplyr` – thelatemail Apr 09 '18 at 00:13
  • thanks I have changed the question topic – itthrill Apr 09 '18 at 00:14
  • 1
    Possible duplicate of - https://stackoverflow.com/questions/25391975/grepl-in-r-to-find-matches-to-any-of-a-list-of-character-strings – thelatemail Apr 09 '18 at 00:16

1 Answers1

1

grepl won't take a vector of strings. You could use map2 from purrr

First create a new function which returns a 1 if there is a substring, 0 otherwise.

new_func <- function(x,y){
if(grepl(x,y,fixed=TRUE)){
  check <- 1
} else{
  check <- 0
}
check
}

Now map2 this function to each pair of strings:

library(purrr)
x <- x %>% 
     mutate(preferred_tag=map2(referred_cat, var_nm, new_func))
John F
  • 994
  • 10
  • 26
  • 1
    `if(grepl(x,y,fixed=TRUE)){ check <- 1 } else{ check <- 0 }` is the same as `check <- as.integer(grepl(x, y, fixed=TRUE))` – Rich Scriven Apr 09 '18 at 00:57