Search code examples
rtexttibblegreplmutate

Replace entire strings in column based on partial match in other column


my issue is similar to this one here (Replace entire strings based on partial match) or this (Replace the entire expression based on a partial match in R)

I think I have a tibble? that is something like this

My tibble enter image description here I tried to use code similar to that of the linked post to change the text in one column based on a partial match in another. it isn't quite working.

free <- c("journal & radius wear damage","residual magnetism","damaged wheel","gouge in edge","wheel seat")

f2 <- tibble(free)
f2 %>%
  mutate(catag = "Uncategorised") %>%
print(f2)
#classify if contains text 'barrel'
f2$catag[grepl("wheel", f2$free)] <- "Wheel related issue"

print(f2)

it found all my matches that contained 'wheel' and changed the 'catag' column to 'wheel related issue' but then made all the other column entries 'na'

not quite right enter image description here

Is there a way to only change the ones that match and leave the ones that don't. I think it is an issue with mutate but this was a way I found to add a column with a constant value.

I tried looking for a solution, but just kept ending up at the same posts.


Solution

  • A full tidyverse solution using mutate(), case_when() and str_detect():

    library(tidyverse)
    
    free <- c('journal & radius wear damage',
              'residual magnetism',
              'damaged wheel',
              'gouge in edge',
              'wheel seat')
    
    # create tibble:
    f2 <- tibble(free)
    
    # conditionally set 'catag':
    f2 |>
      mutate(catag = case_when(str_detect(free, 'wheel') ~ 'Wheel related issue',
                               T ~ 'Uncategorised'))
    

    if_else() is also a possible solution, but doesn't allow for multiple conditions to be evaluated.