Get the strings before the comma with R

后端 未结 5 529
清酒与你
清酒与你 2020-12-31 07:16

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,
P         


        
5条回答
  •  情深已故
    2020-12-31 07:47

    You could use regexpr to find the position of the first comma in each element and use substr to snip them at this:

    x <- c("London, UK", "Paris, France", "New York, USA")
    
    substr(x,1,regexpr(",",x)-1)
    [1] "London"   "Paris"    "New York"
    

提交回复
热议问题