提取字符串中的所有单词和字母簇,然后使用R中的gsub()将每个单词作为单独的数据片段

问题描述 投票:0回答:2

说我们有:

stringTest <- c("Here we have 4 words", "Here we have avwerfaf 4")

预期输出:

“”这里“”我们“”拥有“”单词“”这里“”我们“”拥有“” avwerfaf“

我想使用gsub(),但是其他方法绝对不包括在内。谢谢你们!

r extract gsub
2个回答
1
投票

您可以使用strsplit

result <- unlist(strsplit(stringTest, " |\\d"))
result[result != ""]
#> [1] "Here"     "we"       "have"     "words"    "Here"     "we"      
#> [7] "have"     "avwerfaf"

0
投票
library(tidyverse)

stringTest <- c("Here we have 4 words", "Here we have avwerfaf 4")

gsub(" \\d", replacement = "", stringTest)
© www.soinside.com 2019 - 2024. All rights reserved.