Replacing cells based on string patterns

Hi all,

If I have a table that contains cells "Monitors are bright" "Monitor bright" "The sky is very blue" "The sky is blue".
(the quotation marks represent different cells in the dataframe, but they are all within the same column)

I would like a function/code that is able to make the first two cells = "1", and the second two cells equal "2", based on the words within the cells. So, something that says a cell that contains the words "Monitor" & "Blue" = "1", and cells that contain "sky" & "blue" = "2". I am dealing with a very large dataframe and will likely have 100+ cells to differentiate and catagorise based on the words within them, however if i know how to do it for two words I will be able to apply to more.

Sorry for the ramble and I appreciate the help

Here is one possible solution.

#> Warning: package 'tibble' was built under R version 4.1.2
DF <- data.frame(Text=c("Monitors are bright", "Monitor bright", 
                        "The sky is very blue", "The sky is blue",
                        "other text"))
DF <- DF |> mutate(Text=case_when(
  str_detect(Text, "Monitor") & str_detect(Text, "bright") ~ "1",
  str_detect(Text, "sky") & str_detect(Text, "blue") ~ "2",
  TRUE  ~  Text
#>         Text
#> 1          1
#> 2          1
#> 3          2
#> 4          2
#> 5 other text

Created on 2022-07-14 by the reprex package (v2.0.1)

1 Like

Oo, this looks like it might work! I'll try it very soon.

Sorry to be annoying, how would would it be possible to put the "1" or "2" In a new column rather than replacing the current? Thanks :slight_smile:

To make a new column:

DF <- DF |> mutate(NewColumn = case_when(
  str_detect(Text, "Monitor") & str_detect(Text, "bright") ~ "1",
  str_detect(Text, "sky") & str_detect(Text, "blue") ~ "2",
  TRUE  ~  Text
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.