R Identifing text string within column of dataframe with (AND,OR )

If I understand your questions correctly, I think a good way to go would be using regular expressions, like in this example

library(tidyverse)

sample_df <- data.frame(stringsAsFactors = FALSE,
                        text = c("HIGH VOLUME CREATES NUISANCE TO EVERYONE", "other text"))

sample_df %>% 
    mutate(new_column = if_else(str_detect(text, regex("high.*volume|low.*voice", ignore_case = TRUE)),
                                "VOLUME", NA_character_))
#>                                       text new_column
#> 1 HIGH VOLUME CREATES NUISANCE TO EVERYONE     VOLUME
#> 2                               other text       <NA>

Created on 2019-11-19 by the reprex package (v0.3.0.9000)
If this doesn't solve your problem, then please provide a proper REPRoducible EXample (reprex) illustrating your issue.

1 Like