Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - remove anything after comma from column

Tags:

string

regex

r

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  
like image 652
user3922483 Avatar asked Aug 14 '14 12:08

user3922483


2 Answers

You can use gsub:

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

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

like image 41
martin Avatar answered Sep 23 '22 17:09

martin


You could use gsub() and some regex:

> x <- 'Day, Bobby Jean'
> gsub("(.*),.*", "\\1", x)
[1] "Day"
like image 55
EDi Avatar answered Sep 19 '22 17:09

EDi