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!
This works as well:
If the this was a column in a dataframe, we can use tidyverse.
Just for fun, you can use
strsplit
You can use
gsub
with a bit of regexp :This one works, too :
You could use
regexpr
to find the position of the first comma in each element and usesubstr
to snip them at this: