SQL 的 LIKE 'description%' 语句的 R 等价物是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3539826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the R equivalent of SQL's LIKE 'description%' statement?
提问by Albert Lyu
Not sure how else to ask this but, I want to search for a term within several string elements. Here's what my code looks like (but wrong):
不知道怎么问这个,但是,我想在几个字符串元素中搜索一个术语。这是我的代码的样子(但有误):
inplay = vector(length=nrow(des))
for (ii in 1:nrow(des)) {
if (des[ii] = 'In play%')
inplay[ii] = 1
else inplay[ii] = 0
}
des is a vector that stores strings such as "Swinging Strike", "In play (run(s))", "In play (out(s) recorded)" and etc. What I want inplay to store is a 1s and 0s vector corresponding with the des vector, with the 1s in inplay indicating that the des value had "In play%" in it and 0s otherwise.
des 是一个存储字符串的向量,例如“Swinging Strike”、“In play (run(s))”、“In play (out(s) records)”等。我想要 inplay 存储的是 1s 和 0s向量与 des 向量对应,inplay 中的 1 表示 des 值中有“In play%”,否则为 0。
I believe the 3rd line is incorrect, because all this does is return a vector of 0s with a 1 in the last element.
我相信第三行是不正确的,因为所有这些都是返回一个 0 向量,最后一个元素是 1。
Thanks in advance!
提前致谢!
回答by dnlbrky
The data.table
packagehas syntax that is often similar to SQL. The package includes %like%
, which is a "convenience function for calling regexpr". Here is an example taken from its help file:
该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
回答by doug
The R analog to SQL's LIKE is just R's ordinary indexing syntax.
R 类似于 SQL 的 LIKE 只是 R 的普通索引语法。
The 'LIKE' operator selects data rows from a table by matching string values in a specified column against a user-supplied pattern
“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
In SQL, that would be:
在 SQL 中,这将是:
SELECT * FROM dfx WHERE Colors LIKE ptn3
回答by Vince
Something like regexpr
?
像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
>
or grep
或者 grep
> grep('In play', d)
[1] 2 3
>