Get the strings before the comma with R

后端 未结 5 497
清酒与你
清酒与你 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"
    
    0 讨论(0)
  • 2020-12-31 07:48

    If the this was a column in a dataframe, we can use tidyverse.

    library(dplyr)
    x <- c("London, UK", "Paris, France", "New York, USA")
    x <- as.data.frame(x)
    x %>% separate(x, c("A","B"), sep = ',')
            A       B
    1   London      UK
    2    Paris  France
    3 New York     USA
    
    0 讨论(0)
  • 2020-12-31 07:53

    Just for fun, you can use strsplit

    > x <- c("London, UK", "Paris, France", "New York, USA")
    > sapply(strsplit(x, ","), "[", 1)
    [1] "London"   "Paris"    "New York"
    
    0 讨论(0)
  • 2020-12-31 07:57

    This works as well:

    x <- c("London, UK", "Paris, France", "New York, USA")
    
    library(qdap)
    beg2char(x, ",")
    
    ## > beg2char(x, ",")
    ## [1] "London"   "Paris"    "New York"
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题