0

I have a dataframe as following :

df <- data.frame(month = c("mazda miata 2017", "honda civic 2017"),
                 april = c(.1,.2),
                 may = c(.1,.2)) 

        month april may
1     miata     0.1 0.1
2     civic     0.2 0.2

I would like to rename mazda 3 as mazda and honda civic as honda. How can this be done using dplyr

SNT
  • 1,283
  • 3
  • 32
  • 78

4 Answers4

2

Since you mention dplyr

library(dplyr)
library(tidyr)
df%>%tidyr::separate(month, c("month","Drop"), " ")%>%select(-Drop)
  month april may
1 mazda   0.1 0.1
2 honda   0.2 0.2
BENY
  • 317,841
  • 20
  • 164
  • 234
  • I just realized after your answer on if I have a bigger text and just want that part how to do it. Modified my question. The point being I dont want the first word always.Thank you. – SNT Sep 14 '18 at 19:37
  • @SNT You may need to explain more , do you have something , like Honda CRV 1.1 so you want the CRV out put ? Or maybe AUdi Q5 you want Q5 output ? – BENY Sep 14 '18 at 19:41
  • I actually have it like `"Marlboro Gold Mainline", "Marlboro Red Mainline", "Marlboro Green Mainline","Marlboro Black Menthol", "Marlboro Black Non-Menthol"` . So I want the names to be `Gold,Red,Green,Black Menthol, Black Non-Menthol` – SNT Sep 14 '18 at 19:43
  • @SNT `df%>%tidyr::separate(month, c('Drop','month')," ")%>%select(-Drop)` is this work for you ? – BENY Sep 14 '18 at 19:47
  • Wow :) simply Wow :) Thank you Wen. – SNT Sep 14 '18 at 19:53
  • @ Wen I do run into an issue . Both `"Marlboro Black Menthol", "Marlboro Black Non-Menthol"` return as `Black,Black` whereas I would like them be like `"Black Menthol", "Black Non-Menthol"` – SNT Sep 14 '18 at 20:09
  • @SNT how about `Marlboro Red Mainline` should it be `Red Mainline` – BENY Sep 14 '18 at 20:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/180071/discussion-between-snt-and-wen). – SNT Sep 14 '18 at 20:11
  • @SNT Sorry I can not using the stack chat – BENY Sep 14 '18 at 20:11
  • It comes up as Red and I am fine with it – SNT Sep 14 '18 at 20:12
  • @SNT how about `df%>%tidyr::separate(month, c('Drop','month')," ",extra = 'merge')%>%select(-Drop)` – BENY Sep 14 '18 at 20:16
  • That adds those words but it also add `Mainline` after green and red which I dont want to.The reason being I have two df one of them as `red mainline,green mainline,black menthol and black non-menthol` and then another df I have `red special,green special,black menthol and black non-menthol` . I eventually want to merge these based on `red,green,black menthol,black non-menthol` – SNT Sep 14 '18 at 20:30
  • is their a way I can replace `mainline` and `special` by using gsub or something – SNT Sep 14 '18 at 20:32
  • @SNT gsub("mainline ", "", yourcolum) – BENY Sep 14 '18 at 20:40
  • I did this `gsub("Mainline", "", Secondary_family) %>%` and I get an error `object 'Secondary_family' not found` . Am I doing something wrong with gsub – SNT Sep 14 '18 at 20:48
  • @SNT check this one https://stackoverflow.com/questions/11936339/replace-specific-characters-within-strings – BENY Sep 14 '18 at 21:07
0

Do you need to use dplyr?

Easy enough with gsub:

df$month <- gsub("honda civic", "honda", df$month)
chatwon
  • 80
  • 1
  • 6
0

You can use the following solution to solve your problem:

levels(df$month)[levels(df$month )=="mazda 3"] <- "mazda"
levels(df$month)[levels(df$month )=="honda civic"] <- "honda"
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
Shirin Yavari
  • 626
  • 4
  • 6
0

Try a simple regular expression with str_replace.

In the example below, replace "mazda " and "honda" with anything else you'd like to eliminate. Separate with the OR symbol |:

 df %>% 
   mutate(month = str_replace(month, "mazda |honda ", "") )

       month april may
1 miata 2017   0.1 0.1
2 civic 2017   0.2 0.2
Nettle
  • 3,193
  • 2
  • 22
  • 26