[英]Data frame r and two conditions in grepl
I have got the following data frame我有以下数据框
x <- c("Action", "Adventure", "Animation")
my_text <- c("During check has Animation.", "check none.", "Here is Adventure.")
a <- c(1,2,3)
result <- c(1,5.2,3)
x my_text a result
1 Action During check has Animation. 1 1
2 Adventure check none. 2 5.2
3 Animation Here is Adventure. 3 3
Basically I am trying to use grepl to give me the results column.基本上我试图使用 grepl 给我结果列。 I am ok to use one condition in grepl and check for word "check" and if found produce 5.2.
我可以在 grepl 中使用一个条件并检查单词“check”,如果找到则生成 5.2。 However what I want to is to find "check" but if "during check" found value from a should be used.
然而,我想要的是找到“检查”,但如果“检查期间”从 a 中找到值应该被使用。 So, if "check" then 5.2 but when "during check" value from column a, for anything else value from a as well?
那么,如果“检查”然后是 5.2,但是当“检查期间”来自 a 列的值时,还有来自 a 的其他值吗?
You can try using lookbehind.您可以尝试使用lookbehind。 With your data:
使用您的数据:
df = data.frame(x=x,my_text=my_text,a=a,stringsAsFactors = FALSE)
df$result = ifelse(grepl("(?<!during )\\bcheck",df$my_text,perl=TRUE,ignore.case = TRUE),
5.2,df$a)
> df
x my_text a result
1 Action During check has Animation. 1 1.0
2 Adventure check none. 2 5.2
3 Animation Here is Adventure. 3 3.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.