Get the strings before the comma with R

后端 未结 5 514
清酒与你
清酒与你 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 08:02

    You can use gsub with a bit of regexp :

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

    This one works, too :

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

提交回复
热议问题