0

I have a data frame 'fol_all' with field 'folno'. I have a list called string_detect with list of strings I want to flag in fol_all if they are present in folno.

folno <- c('123denied', 'attached_as_test', 'dept_224', 'bulked_up', 'wsd2273')
fol_all <- data.frame(folno)

string_detect <- c('denied', 'attach', 'bulk')

folno <- folno %>% 
  mutate(folno_flag = case_when(str_detect(folno, string_detect)) == T ~ 1, T == 0)

In this example, rows 1, 2, and 4 would return a 1 and the rest a 0.

I get the following error - please help.

Error in UseMethod("mutate") : 
  no applicable method for 'mutate' applied to an object of class "character"

1 Answer 1

1

You should first create the regex-pattern to be applied to the column, e.g.

 string_detect <- paste(c('denied', 'attach', 'bulk'), collapse = "|")

Then you need to adjust your case_when code:

folno <- fol_all %>% 
  mutate(folno_flag = case_when(
    str_detect(folno, string_detect) ~ 1, 
    TRUE ~ 0 ))

Result:

             folno folno_flag
1        123denied          1
2 attached_as_test          1
3         dept_224          0
4        bulked_up          1
5          wsd2273          0

You do not need str_detect(folno, string_detect)) == T because the statement basically says, if str_detect(folno, string_detect)) evaluates to True, then 1, 0 otherwise.

Whole code:

folno <- c('123denied', 'attached_as_test', 'dept_224', 'bulked_up', 'wsd2273')
fol_all <- data.frame(folno)

string_detect <- paste(c('denied', 'attach', 'bulk'), collapse = "|")

fol_all %>% 
  mutate(folno_flag = case_when(
    str_detect(folno, string_detect) ~ 1, 
    TRUE ~ 0 ))
Sign up to request clarification or add additional context in comments.

4 Comments

Thank Julian. I get the following error. Error in UseMethod("mutate") : no applicable method for 'mutate' applied to an object of class "c('double', 'numeric')" In addition: Warning message: In stri_detect_regex(string, pattern, negate = negate, opts_regex = opts(pattern)) : longer object length is not a multiple of shorter object length
I added the whole code I used using your example, it works for me without the error.
TY - that works . How would I modify to ignore case?
I tried the following to ignore case, but all the folno_flag came out as 0. fol_all %>% mutate(folno_flag = case_when( str_detect(folno, fixed(string_detect, ignore_case = T)) ~ 1, TRUE ~ 0 ))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.