R将单词与数字分开 [英] R separate words from numbers in string

查看:88
本文介绍了R将单词与数字分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要清理一些包含单词和数字或仅包含数字的数据字符串。

I need to clean up some data strings that have words and numbers or just numbers.

下面是一个玩具样本

library(tidyverse)

c("555","Word 123", "two words 123", "three words here 123") %>%  
sub("(\\w+) (\\d*)",  "\\1|\\2", .)

结果是这样的:

[1] "555"                  "Word|123"             "two|words 123"        "three|words here 123"

,但我想将'|'放在最后一组数字之前如下所示

but I want to place the '|' before the last set of numbers like shown below

[1] "|555"                  "Word|123"             "two words|123"        "three words here|123"


推荐答案

我们可以使用 sub 以匹配零个或多个空格( \\s * ),然后是我们捕获为一组的数字( (\\d)),并在替换中使用 | ,后跟反向引用( \\捕获的组中的1

We can use sub to match zero or more spaces (\\s*) followed by a digit we capture as a group ((\\d)) and in the replacement use the | followed by the backreference (\\1) of the captured group

sub("\\s*(\\d)", "|\\1", v1)
#[1] "|555"                 "Word|123"            
#[3] "two words|123"        "three words here|123"



数据



data

v1 <- c("555","Word 123", "two words 123", "three words here 123")

这篇关于R将单词与数字分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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