rename and mutate character values

Hey, I´m trying to rename some string values ( that contain specific parts) to another strings. Tried in the starwars tibble (dyplr). I want to rename many of them differently.

.*Na is for Naboo
.*oo is for Tatooine

df_test1 <- starwars  %>% 
  mutate(new_homeworld = gsub ("[.*Na.* | .*oo.*]", "A" ,homeworld)
df_test1

It did not work

Also tried

df_test2 <- starwars  %>% 
  mutate(new_homeworld = case_when(homeworld == ".*Na.*" ~ "A",
                                      homeworld == ".*oo.*" ~ "B",
                                      TRUE ~ "C")) 

Just get " C" for all values

Any thoughts on that?

I added a letter to each matching fragment, so it didnt also tag homeworlds that arent Naboo or Tatooine

df_test1 <- starwars %>% select(name,homeworld) %>%
  mutate(is_naboo_or_tatooine = str_detect(homeworld,pattern="Nab|too"),
         new_homeworld = case_when(is_naboo_or_tatooine ~ "A",
                                   TRUE ~ homeworld))
         
         df_test1

Thanks. But it did not work for me.

Just tried, but it also did not work usin ".*"

df_test3 <- starwars %>%
mutate(new_homeworld = gsub (".Nab.", "A" ,homeworld),
(new_homeworld = gsub (".too.", "B" ,homeworld),
new_homeworld = gsub (".Ste.", "C" ,homeworld)))

df_test3

I don't what you mean ?
did you get an error ? what error did you get ?
did it run without error, but give an improper result ? how so ?

ps. it might be helpful to know how to properly format code and console output that you post here. Using proper code formatting makes the site easier to read, prevents confusion (unformatted code can get garbled by the forum software :anguished:), and is generally considered the polite thing to do. Check out this FAQ to find out how — it's as easy as the click of a button! :grinning::

1 Like

It wasn´t mutating in the whole tibble. But I figured, it was the select function. I just removed it.

Tried two diffrent ways and they worked!

df_test4 <- starwars %>% 
    mutate(is_naboo_or_tatooine = str_detect(homeworld,".*Nab.*|.*too.*"),
           is_stewjon_or_eriadu = str_detect(homeworld,".*Ste.*|.*Eri.*"),
           new_homeworld = case_when(is_naboo_or_tatooine ~ "A",
                                     is_stewjon_or_eriadu ~ "B",
                                     TRUE ~ homeworld))

  df_test5 <- starwars %>% 
    mutate(new_homeworld = case_when(str_detect(homeworld,".*Nab.*|.*too.*") ~ "A",
                                     str_detect(homeworld,".*Ste.*|.*Eri.*") ~ "B",
                                     TRUE ~ homeworld))

What do you think about them? You helped me a lot with the str_detect. Thanks

I think the additional punctuation marks .* etc are superflous.

df_test5 <- starwars %>% 
  mutate(new_homeworld = case_when(str_detect(homeworld,".*Nab.*|.*too.*") ~ "A",
                                   str_detect(homeworld,".*Ste.*|.*Eri.*") ~ "B",
                                   TRUE ~ homeworld))
df_test6 <- starwars %>% 
  mutate(new_homeworld = case_when(str_detect(homeworld,"Nab|too") ~ "A",
                                   str_detect(homeworld,"Ste|Eri") ~ "B",
                                   TRUE ~ homeworld))

identical(df_test5$new_homeworld,
               df_test6$new_homeworld)

or at least for the starwars input

This topic was automatically closed 21 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.