R - remove anything after comma from column

后端 未结 5 1683
死守一世寂寞
死守一世寂寞 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 01:04

    You could use gsub() and some regex:

    > x <- 'Day, Bobby Jean'
    > gsub("(.*),.*", "\\1", x)
    [1] "Day"
    

提交回复
热议问题