fake fake
fake fake

Reputation: 15

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.

Upvotes: 0

Views: 231

Answers (2)

jamespryor
jamespryor

Reputation: 61

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.

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 389225

You may use ifelse to conditionally change the values.

f2 <- transform(f2, catag = ifelse(grepl("wheel", free), 
                        "Wheel related issue", "Uncategorised"))
f2

#                       free               catag
#1 journal & radius wear damage       Uncategorised
#2           residual magnetism       Uncategorised
#3                damaged wheel Wheel related issue
#4                gouge in edge       Uncategorised
#5                   wheel seat Wheel related issue

Similarly, using dplyr functions :

library(dplyr)

f2 %>%
  mutate(catag = if_else(grepl("wheel", free), 
                         "Wheel related issue", "Uncategorised"))

data

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

Upvotes: 0

Related Questions