R - remove anything after comma from column

后端 未结 5 1668
死守一世寂寞
死守一世寂寞 2020-12-02 00:18

I\'d like to strip this column so that it just shows last name - if there is a comma I\'d like to remove the comma and anything after it. I have data column that is a mix of

相关标签:
5条回答
  • 2020-12-02 00:50

    You can use gsub:

    gsub(",.*", "", c("last only", "last, first"))
    # [1] "last only" "last"
    

    ",.*" says: replace comma (,) and every character after that (.*), with nothing "".

    0 讨论(0)
  • 2020-12-02 00:57
     str1 <- c("Sample, A", "Tester", "Wifred, Nancy", "Day, Bobby Jean", "Morris")
     library(stringr)
      str_extract(str1, perl('[A-Za-z]+(?=(,|\\b))'))
     #[1] "Sample" "Tester" "Wifred" "Day"   "Morris"  
    

    Match alphabets [A-Za-z]+ and extract those which are followed by , or word boundary.

    0 讨论(0)
  • 2020-12-02 01:04

    You could use gsub() and some regex:

    > x <- 'Day, Bobby Jean'
    > gsub("(.*),.*", "\\1", x)
    [1] "Day"
    
    0 讨论(0)
  • 2020-12-02 01:05

    Also try strsplit:

    string <- c("Sample, A", "Tester", "Wifred, Nancy", "Day, Bobby Jean", "Morris")
    
    sapply(strsplit(string, ","), "[", 1)
    #[1] "Sample" "Tester" "Wifred" "Day"    "Morris"
    
    0 讨论(0)
  • 2020-12-02 01:10

    This is will work

    a <- read.delim("C:\\Desktop\\a.csv", row.names = NULL,header=TRUE, 
                     stringsAsFactors=FALSE,sep=",")
    a=as.matrix(a)
    Data=str_replace_all(string=a,pattern="\\,.*$",replacement=" ")
    
    0 讨论(0)
提交回复
热议问题