Home > Software engineering >  Using str_detect on data frame column values
Using str_detect on data frame column values

Time:05-02

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"

CodePudding user response:

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 ))
  • Related