【发布时间】:2019-04-25 19:12:56
【问题描述】:
我需要清理一些包含单词和数字或只有数字的数据字符串。
下面是玩具样品
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"
但我想放置“|”在最后一组数字之前,如下所示
[1] "|555" "Word|123" "two words|123" "three words here|123"
【问题讨论】:
-
试试
sub("(\\w+ )?(\\d)", "\\1|\\2", v1) -
@akrun 这行得通,你能把它作为答案让我接受吗?
标签: r regex data-cleaning