微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SQL的LIKE’description%’语句的R等价物是什么?

不知道怎么回事,但是我想在几个字符串元素中搜索一个术语.这是我的代码看起来(但错误):
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)recording)”等等.我想要播放的是1s和0s与des矢量相对应的矢量,其中1s在播放中表示des值在其中为“In play%”,否则为0.

我相信第三行是不正确的,因为所有这一切都是返回0的向量,在最后一个元素中有1.

提前致谢!

解决方法

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

原文地址:https://www.jb51.cc/mssql/82275.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐