我有一个数据库结构-下面是缩略版

structure(list(sex1 = c("totalmaleglobal", "totalfemaleglobal",
"totalglobal", "totalfemaleGSK", "totalfemaleglobal",
"totalfemaleUN")), .Names = "sex1", row.names = c(NA, 6L),
class="data.frame")

我想提取单词“total”,“totalmale”,“totalfemale”

怎么做呢?

我用以下代码尝试了正则表达式
pattern1=c("total")
pattern2=c("totalmale")
pattern3=c("totalfemale")

daly$sex <- str_extract(daly$sex1,pattern1)
daly$sex <- str_extract(daly$sex1,pattern2)
daly$sex <- str_extract(daly$sex1,pattern3)

但是它给了我NA。

最佳答案

也许

library(stringr)
daly$sex <- str_extract(daly$sex1,paste(rev(mget(ls(pattern = "pattern\\d+"))), collapse="|"))
daly
#                sex1         sex
# 1   totalmaleglobal   totalmale
# 2 totalfemaleglobal totalfemale
# 3       totalglobal       total
# 4    totalfemaleGSK totalfemale
# 5 totalfemaleglobal totalfemale
# 6     totalfemaleUN totalfemale

关于regex - 从字符串中提取匹配的单词,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39406224/

10-09 17:08