r中grepl内的数字比较

时间:2016-05-27 11:37:07

标签: r dataframe comparison grepl

我试图在我的数据帧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 %"

有任何纠正此问题的建议吗?

2 个答案:

答案 0 :(得分:1)

如何使用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)

答案 1 :(得分:1)

grepl("[0-9]",df$col1)只搜索df$col1内的数字,并在找到数字时返回TRUE。将TRUE转换为数字只会产生1.总是小于15。

所以这并不是你真正想要的。正如lmo所提到的,你可能希望通过像gsub这样的东西来提取实际数字。