SWR
SWR

Reputation: 507

R cleaning up a column

I have - from my perspective - quite a tricky task: I have a large dataset about VW cars. One column contains the car models. I only would like to have there VW + mode (e.g. VW Golf or VW Passat). I could delete all the stuff I don't need with gsub, but that is not efficient in my eyes and would need probably 50 lines of code.

What would be an easy and efficent way to solve this problem?

enter image description here

Upvotes: 0

Views: 82

Answers (1)

agstudy
agstudy

Reputation: 121568

It is not clear what you have tried with gsub, But I would do something like this:

gsub('(^VW \\w+).*','\\1',modell)

Using some data :

modell <- c('VW Caddy Life 1.6 ',
'VW Passat 3.2',
'VW Bora 2.8',
'VW Golf 1.4',            
'VW Passat (Kombi)')

gsub('(^VW \\w+).*','\\1',modell)
[1] "VW Caddy"  "VW Passat" "VW Bora"   "VW Golf"   "VW Passat"

Upvotes: 1

Related Questions