R - 从列中删除逗号后的任何内容 [英] R - remove anything after comma from column

查看:51
本文介绍了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  

推荐答案

你可以使用 gsub() 和一些正则表达式:

You could use gsub() and some regex:

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

这篇关于R - 从列中删除逗号后的任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆