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
You can use gsub:
gsub(",.*", "", c("last only", "last, first"))
# [1] "last only" "last"
",.*"
says: replace comma (,) and every character after that (.*), with nothing ""
.
You could use gsub() and some regex:
> x <- 'Day, Bobby Jean'
> gsub("(.*),.*", "\\1", x)
[1] "Day"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With