jeudi 6 février 2020

%like% with multiple patterns in r

Is it possible to use multiple pattern with %like% in a nested ifelse? If not, what would be the alternative?

fruits<-c("apple", "pineapple", "grape", "avocado","banana")

color <-c("red","yellow","purple", "green","yellow")

mydata = data.frame(fruits=fruits,color=color ) 


mydata %>%
  mutate(group = ifelse(fruits %like% c("%pple%","%vocado%"), "group 1",
                           ifelse(fruits %like% c("%anana%","%grape%"), "group 2", "group 3")))

When I try the code above, I get the following error:

Warning messages:
1: In grep(pattern, levels(vector)) :
  argument 'pattern' has length > 1 and only the first element will be used
2: In grep(pattern, levels(vector)) :
  argument 'pattern' has length > 1 and only the first element will be used

Any guidance is appreciated. Thank you!

Aucun commentaire:

Enregistrer un commentaire