我有一个数据库结构 – 下面的缩写版本
structure(list(sex1 = c("totalmaleglobal","totalfemaleglobal","totalglobal","totalfemaleGSK","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