从列中提取第一个单词并插入新列 [英] Extract first word from a column and insert into new column

查看:65
本文介绍了从列中提取第一个单词并插入新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面有一个数据框,想提取第一个单词并将其插入新列中

I have a dataframe below and want to extract the first word and insert it into a new column

Dataframe1:

COL1
Nick K Jones
Dave G Barros
Matt H Smith

将其转换为以下内容:

Dataframe2:
COL1              COL2
Nick K Jones      Nick
Dave G Barros     Dave
Matt H Smith      Matt


推荐答案

您可以使用正则表达式((([A-Za-z] +) ([[: alpha:]]))(\\w +) )抓取第一个单词

You can use a regex ("([A-Za-z]+)" or "([[:alpha:]]+)"or "(\\w+)") to grab the first word

Dataframe1$COL2 <- gsub("([A-Za-z]+).*", "\\1", Dataframe1$COL1)

这篇关于从列中提取第一个单词并插入新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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