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.
library(tidyverse)
#> 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
))
DF
#> Text
#> 1 1
#> 2 1
#> 3 2
#> 4 2
#> 5 other text