R - remove anything after comma from column

20,951

Solution 1

You could use gsub() and some regex:

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

Solution 2

You can use gsub:

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

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

Share:
20,951
user3922483
Author by

user3922483

Updated on July 15, 2022

Comments

  • user3922483
    user3922483 almost 2 years

    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 just last names and last, first. The data looks as follows:

    Last Name  
    Sample, A  
    Tester  
    Wilfred, Nancy  
    Day, Bobby Jean  
    Morris