2

I have a dataframe where one column is pattern and another is string. I want to iterate over each row check if the rows's string contains row's pattern and update the Matches column with T/F status I tried

df<- df%>%mutate(Matches=grepl(pattern,string))

and got the following error.

argument 'pattern' has length > 1 and only the first element will be used

I understand that in the above code grepl is trying to read all rows of pattern column instead of the current row.

Is there any function which does this job or do I need to use a for loop and manually iterate over each row?

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
vijayvithal
  • 551
  • 1
  • 5
  • 13
  • Please show a small reproducible example and expeced output. If each row pattern should be matched with 'string' in the same row, then try `df$Matches <- mapply(grepl, df$pattern, df$string)` – akrun Dec 29 '16 at 05:42
  • Or if you need to use `dplyr`, then `df %>% rowwise() %>% mutate(Matches = grepl(pattern, string))` – akrun Dec 29 '16 at 05:45

1 Answers1

12

If we need to compare the 'string' with 'pattern' in each row, then use rowwise() from dplyr

library(dplyr)
df %>% 
    rowwise() %>% 
    mutate(Matches = grepl(pattern, string))    
# A tibble: 3 × 3
#  pattern string Matches
#    <chr>  <chr>   <lgl>
#1      sl  sling    TRUE
#2      ab   dare   FALSE
#3      cd   care   FALSE

It can also be done with mapply from base R

df$Matches <- mapply(grepl, df$pattern, df$string)

data

df <- data.frame(pattern = c("sl", "ab", "cd"),
       string = c("sling", "dare", "care"), stringsAsFactors=FALSE)
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    Thanks, I was not aware of rowwise function – vijayvithal Dec 29 '16 at 06:11
  • 3
    If you want to stay with pure-`dplyr`, you can get better speed without `rowwise()` using a hybrid of these approaches: `df %>% mutate(Matches = mapply(grepl, pattern, string))`. – r2evans Dec 29 '16 at 07:34