Get the strings before the comma with R

user2855907 picture user2855907 · Oct 11, 2013 · Viewed 26.3k times · Source

I am a beginner with R. Now, I have a vector in a data.frame like this

city
Kirkland,
Bethesda,
Wellington,
La Jolla,
Berkeley,
Costa, Evie KW172NJ
Miami,
Plano,
Sacramento,
Middletown,
Webster,
Houston,
Denver,
Kirkland,
Pinecrest,
Tarzana,
Boulder,
Westfield,
Fair Haven,
Royal Palm Beach, Fl
Westport,
Encino,
Oak Ridge,

I want to clean it. What I want is all the city names before the comma. How can I get the result in R? Thanks!

Answer

juba picture juba · Oct 11, 2013

You can use gsub with a bit of regexp :

cities <- gsub("^(.*?),.*", "\\1", df$city)

This one works, too :

cities <- gsub(",.*$", "", df$city)