R语言 返回任何以空格+指定字母+数字开头的单词

rjee0c15  于 2023-05-26  发布在  其他
关注(0)|答案(1)|浏览(178)

我如何才能只返回匹配以下模式的单词:以空格+“t”或“r”开头,后面直接跟R中的任何数字,包括负数或小数,并且在不满足这些条件的情况下返回NA?

编辑包括可能的十进制数

例如:

testvec <- c("random stuff here","words 10293","random t101rando 101 000","r10000","stuff i-10283","word1 t-12.34 stuff rand10293","random100 u-1000"," r10.0 x ","test x-2930"," T r.1234567","testword120num")

使用上述测试数据的预期结果(testvec):

desired_result <- c(NA,NA,"t101rando",NA,NA,"t-12.34",NA,"r10.0",NA,"r.1234567",NA)
ie3xauqp

ie3xauqp1#

尝试:

regmatches(testvec, regexpr("(?<= )[tr]-?\\.?\\d\\S*", testvec, perl=TRUE), TRUE) <- ""
testvec
# [1] ""          ""          "t101rando" ""          ""          "t-12.34"  
# [7] ""          "r10.0"     ""          "r.1234567" ""

""而不是NA

相关问题