r中grepl内部的数字比较

Numeric comparisons inside grepl in r

我试图在我的数据框 df 的 col1 中找到小于指定数字(例如:- 15)的唯一值

我尝试了下面的代码,

unique(df[grepl("increased by 1", df$col1) &
( as.numeric(grepl("[0-9]",df$col1 )) <15),]$col1)

但似乎只有第一个 grepl 有效。

[1] "increased by 17 %" "increased by 10 %" "increased by 16 %" "increased by 1 %"  "increased by 14 %"
[6] "increased by 13 %" "increased by 12 %" "increased by 15 %" "increased by 11 %" "increased by 18 %"

有任何解决此问题的建议吗?

gsub 创建一个新变量怎么样?

# get vector in R
temp <- c("increased by 17 %", "increased by 10 %", "increased by 16 %",   
          "increased by 1 %",  "increased by 14 %", "increased by 13 %", 
          "increased by 12 %", "increased by 15 %", "increased by 11 %", 
          "increased by 18 %")
# extract value as numeric
myValues <- as.numeric(gsub("increased by ([0-9]+) %", "\1", temp))

生成逻辑向量

myValues > 15

提取值

myValues[myValues > 15]

获取指数

which(myValues > 15)

grepl("[0-9]",df$col1) 只搜索 df$col1 中的数字,returns TRUE 找到数字时。将 TRUE 转换为数字只会产生 1。它始终小于 15。

所以这并不是您真正要找的。正如 lmo 提到的,您可能想通过 gsub.

之类的方式提取实际数字