R programming- how to rename/change values

new to R

i need to change the values for the variable location (basically anything truncated will be the full value)

data:

  location
   s. sj
  S. SJ @ Ashley Store
  S. SJ @ Ashley
  Pleasanton
  pleas
  Pleasanton @ Ranch 99

want:

 location
 S. SJ @ Ashley Store
 S. SJ @ Ashley Store
 S. SJ @ Ashley Store
 Pleasanton @ Ranch 99
 Pleasanton @ Ranch 99
 Pleasanton @ Ranch 99

thanks!

This works for these values:

library(tidyverse)

df <- tibble::tribble(
                ~location,
                  "s. sj",
   "S. SJ @ Ashley Store",
         "S. SJ @ Ashley",
             "Pleasanton",
                  "pleas",
  "Pleasanton @ Ranch 99"
  )

df %>% 
  mutate(location2 = case_when(
    location %in% c("s. sj", "S. SJ @ Ashley Store", "S. SJ @ Ashley") ~ "S. SJ @ Ashley Store",
    location %in% c("Pleasanton", "pleas", "Pleasanton @ Ranch 99") ~ "Pleasanton @ Ranch 99",
    TRUE ~ NA_character_
  ))

Thanks for the quick response.

But is there something more robust? because with a larger dataset there might be other truncated values that I am not aware and those will need full value.

For example anthing starting with a "S" or "s" will be "S. SJ @ Ashley Store"

df %>% 
  mutate(location2 = case_when(
    str_starts(location, c("s", "S", "S.")) ~ "S. SJ @ Ashley Store", # not sure why the dot is needed for the second s
    str_starts(location, c("p", "P")) ~ "Pleasanton @ Ranch 99",
    TRUE ~ NA_character_
  ))

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.