0

I have a column containing the brand and model of cars. How to keep only the brand and remove the model? In the original dataset, the brand and the mode were separated by a space. So I tried this:

carprice$CarName=gsub(pattern = " *", replacement = "",carprice$CarName)

What happened was the space got eliminated and now the brand and the model names are concatenated. I am not able to even undo it.

  • 1
    Essentially the same as https://stackoverflow.com/questions/15895050/using-gsub-to-extract-character-string-before-white-space-in-r – thelatemail Nov 22 '17 at 23:30
  • `gsub("(.*)\\s.*", "\\1", carprice$CarName)` – zacdav Nov 22 '17 at 23:30
  • Your attempt might work if you used `" .*"` as the pattern. At the moment you're matching zero or more spaces only. Adding `.` means you match a space followed by zero or more of any character. – Marius Nov 23 '17 at 00:02
  • @Marius Now I don't think " .*" will work because there is no space in the names anymore. As I said the Brand Name and the Model Name are concatenated. I now need only the brand name or a way to undo the change so that I can use the gsub command with " .*" – Prajwal Acharya Nov 23 '17 at 09:39
  • @zacdav Thank you. May I know What your code does? Cause it did not work for me. – Prajwal Acharya Nov 23 '17 at 09:50
  • Start again then.... My code removes the space and everything after – zacdav Nov 23 '17 at 09:51
  • @zacdav I have come very far. I don't think start over. Any other way where I can just extract the brand names? For example the elements are like this: [1] "alfa-romerogiulia" "alfa-romerostelvio" "alfa-romeroQuadrifoglio" [4] "audi100ls" "audi100ls" "audifox". I just need alfa-romero, audi – Prajwal Acharya Nov 23 '17 at 10:02

0 Answers0