Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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  
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
614 views
Welcome To Ask or Share your Answers For Others

1 Answer

You could use gsub() and some regex:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...