SQL中的LIKE 'description%'语句在R中的等效语句是什么?

20

我不确定如何更好地提问,但我希望在多个字符串元素中搜索一个术语。这是我的代码(但是错误的):

inplay = vector(length=nrow(des))
for (ii in 1:nrow(des)) {
 if (des[ii] = 'In play%')
  inplay[ii] = 1
 else inplay[ii] = 0
}

des是一个向量,其中存储了字符串,例如"Swinging Strike"、"In play (run(s))"、"In play (out(s) recorded)"等等。我希望inplay也能存储一个向量,该向量与des向量对应,其中1表示des值中包含"In play%",否则为0。

我认为第三行代码是不正确的,因为它只返回一个向量,其中所有元素都是0,最后一个元素是1。

提前感谢!

4个回答

26

data.table 的语法通常与 SQL 相似。该包包括了 %like%,它是一个 "用于调用 regexpr 的便利函数"。以下是其帮助文件中的示例:

## Create the data.table:
DT = data.table(Name=c("Mary","George","Martha"), Salary=c(2,3,4))

## Subset the DT table where the Name column is like "Mar%":
DT[Name %like% "^Mar"]
##      Name Salary
## 1:   Mary      2
## 2: Martha      4

3
最近,我通常使用 %ilike%,这是一个不区分大小写的版本。 - dnlbrky
要进行不区分大小写的搜索,请使用:DT[Name %like% "(?i)mar"]DT[Name %ilike% "mar"] - San

19

R中类似于SQL的LIKE操作符,实际上就是R普通的索引语法。

'LIKE'操作符通过将指定列中的字符串值与用户提供的模式进行匹配来选择数据表中的数据行。

> # create a data frame having a character column
> clrs = c("blue", "black", "brown", "beige", "berry", "bronze", "blue-green", "blueberry")
> dfx = data.frame(Velocity=sample(100, 8), Colors=clrs)
> dfx
            Velocity    Colors
        1       90       blue
        2       94      black
        3       71      brown
        4       36      beige
        5       75      berry
        6        2     bronze
        7       89    blue-green
        8       93    blueberry

> # create a pattern to use (the same as you would do when using the LIKE operator)
> ptn = '^be.*?'  # gets beige and berry but not blueberry
> # execute a pattern-matching function on your data to create an index vector
> ndx = grep(ptn, dfx$Colors, perl=T)
> # use this index vector to extract the rows you want from the data frome:
> selected_rows = dfx[ndx,]
> selected_rows
   Velocity Colors
     4       36  beige
     5       75  berry 

在SQL中,这将是:
SELECT * FROM dfx WHERE Colors LIKE ptn3

你为什么要用 fnx 包装 grep() 函数? - Vince
我最初考虑的是编写一个以数据框作为参数的函数,而不仅仅是一个一维向量。无论如何,现在已经编辑掉了函数包装器。 - doug
这个代码是可以运行的,但是假设我想要在向量inplay中存储一个1,如果行号包含在ndx中,则存储为1,否则存储为0。其中向量inplay的长度与dfx相同。我该如何实现?我正在尝试使用IF和ELSE语句,但是我无法让它正常工作。提前感谢您的帮助! - Albert Lyu
我已经理解了。假设SL是存储“Swinging Strike”、“In play(run(s))”等的矩阵。ndx = grep('^In play.*?', SL$des, perl=T) SL <- transform(SL, inplay=mat.or.vec(1,nrow(SL))[des]) SL$inplay <- replace(SL$inplay, ndx, 1)谢谢! - Albert Lyu
刚看到你的评论——所以你解决了上面的问题“[假设我想存储....]”? - doug

3

类似于regexpr的东西吗?

> d <- c("Swinging Strike", "In play (run(s))", "In play (out(s) recorded)")
> regexpr('In play', d)
[1] -1  1  1
attr(,"match.length")
[1] -1  7  7
> 

或者 grep
> grep('In play', d)
[1] 2 3
> 

0

自从 stringr 1.5.0 版本以来,你可以使用 str_like 函数,它遵循 SQL 的 LIKE 结构:

library(stringr)

fruit <- c("apple", "banana", "pear", "pineapple")
str_like(fruit, "app%")
#[1]  TRUE FALSE FALSE FALSE

不仅包含%,还包括其他几个运算符(请参见?str_like)。

  • 必须完全匹配字符串

  • _⁠ 匹配一个字符(类似于 .)

  • ⁠%⁠ 匹配任意数量的字符(类似于 ⁠.*⁠)

  • ⁠%⁠ 和 ⁠_⁠ 匹配字面值 ⁠%⁠ 和 ⁠_⁠

  • 默认情况下,匹配不区分大小写


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接