根据列中的部分字符串匹配选择数据框行

Selecting data frame rows based on partial string match in a column

提问人:Asda 提问时间:10/24/2012 最后编辑:HenrikAsda 更新时间:10/4/2020 访问量:458873

问:

我想根据列中字符串的部分匹配从数据框中选择行,例如列“x”包含字符串“has”。使用 - 如果它有语法 - 我会做这样的事情:sqldflike

select * from <> where x like 'hsa'.

很遗憾,不支持该语法。sqldf

或类似:

selectedRows <- df[ , df$x %like% "hsa-"]

这当然是行不通的。

有人可以帮我解决这个问题吗?

r 正则表达式 字符串 匹配 子集

评论

6赞 A5C1D2H2I1M1N2O1R2T1 10/24/2012
你能发布几行数据吗,最好使用类似的东西。dput(head(conservedData))

答:

212赞 A5C1D2H2I1M1N2O1R2T1 10/24/2012 #1

我注意到您在当前的方法中提到了一个函数。我不知道这是否是对来自“data.table”的引用,但如果是,您绝对可以按如下方式使用它。%like%%like%

请注意,对象不必是 a(但也要记住,s 和 s 的子集方法并不相同):data.tabledata.framedata.table

library(data.table)
mtcars[rownames(mtcars) %like% "Merc", ]
iris[iris$Species %like% "osa", ]

如果这就是你所拥有的,那么也许你只是混淆了子集数据的行和列位置。


如果您不想加载包,可以尝试使用 来搜索您正在匹配的字符串。下面是数据集的一个示例,其中我们匹配行名包含“Merc”的所有行:grep()mtcars

mtcars[grep("Merc", rownames(mtcars)), ]
             mpg cyl  disp  hp drat   wt qsec vs am gear carb
# Merc 240D   24.4   4 146.7  62 3.69 3.19 20.0  1  0    4    2
# Merc 230    22.8   4 140.8  95 3.92 3.15 22.9  1  0    4    2
# Merc 280    19.2   6 167.6 123 3.92 3.44 18.3  1  0    4    4
# Merc 280C   17.8   6 167.6 123 3.92 3.44 18.9  1  0    4    4
# Merc 450SE  16.4   8 275.8 180 3.07 4.07 17.4  0  0    3    3
# Merc 450SL  17.3   8 275.8 180 3.07 3.73 17.6  0  0    3    3
# Merc 450SLC 15.2   8 275.8 180 3.07 3.78 18.0  0  0    3    3

再举一个例子,使用数据集搜索字符串:irisosa

irisSubset <- iris[grep("osa", iris$Species), ]
head(irisSubset)
#   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1          5.1         3.5          1.4         0.2  setosa
# 2          4.9         3.0          1.4         0.2  setosa
# 3          4.7         3.2          1.3         0.2  setosa
# 4          4.6         3.1          1.5         0.2  setosa
# 5          5.0         3.6          1.4         0.2  setosa
# 6          5.4         3.9          1.7         0.4  setosa

对于您的问题,请尝试:

selectedRows <- conservedData[grep("hsa-", conservedData$miRNA), ]

评论

0赞 nico 10/24/2012
+1:另请注意,它支持正则表达式,因此您可能需要改用 grep for。grep^hsa-
3赞 Stephan Kolassa 10/25/2012
@nico:其实它来源于ed命令g/re/p(全局/正则表达式/print),它只向正则表达式大师透露了它的真正威力-fu ;-): en.wikipedia.org/wiki/Grepgrep
0赞 nigus21 2/13/2020
@A5C1D2H2I1M1N2O1R2T1 很好的答案!有没有办法使用 %like% 来搜索同时出现的两个字符串(例如在数据帧的一行中出现的“pet”和“pip”作为“peter piper”)?
23赞 user1609452 10/24/2012 #2

LIKE应该在SQLite中工作:

require(sqldf)
df <- data.frame(name = c('bob','robert','peter'),id=c(1,2,3))
sqldf("select * from df where name LIKE '%er%'")
    name id
1 robert  2
2  peter  3

评论

0赞 Suat Atan PhD 1/12/2017
SQLDF 最适合列出。但是,它不能删除行。
1赞 rgalbo 3/28/2017
为什么这里加载了 R 包require()
0赞 bartektartanus 8/18/2017
因为它不是标准的 R 库,你必须手动安装它,然后使用函数加载。require
97赞 Sam Firke 7/7/2015 #3

尝试从 stringr 包中尝试,该包检测字符串中是否存在模式。str_detect()

下面是一种方法,它也包含管道和 dplyr 包:%>%filter()

library(stringr)
library(dplyr)

CO2 %>%
  filter(str_detect(Treatment, "non"))

   Plant        Type  Treatment conc uptake
1    Qn1      Quebec nonchilled   95   16.0
2    Qn1      Quebec nonchilled  175   30.4
3    Qn1      Quebec nonchilled  250   34.8
4    Qn1      Quebec nonchilled  350   37.2
5    Qn1      Quebec nonchilled  500   35.3
...

这将筛选示例 CO2 数据集(随 R 一起提供)的行,其中 Treatment 变量包含子字符串“non”。您可以调整是查找固定匹配项还是使用正则表达式 - 请参阅 stringr 包的文档。str_detect

评论

1赞 Bemipefe 4/18/2019
您也可以像这样使用 trc_detect 函数myDataFrame[str_detect(myDataFrame$key, myKeyPattern),]
0赞 Martin 8/20/2021
@Bemipefe你不是说str_detect函数而不是trc_detect吗?
1赞 Bemipefe 8/22/2021
@Martin 是的,你是对的。这是一个错别字。
7赞 AlexB 10/4/2020 #4

另一种选择是简单地使用函数:grepl

df[grepl('er', df$name), ]
CO2[grepl('non', CO2$Treatment), ]

df <- data.frame(name = c('bob','robert','peter'),
                 id = c(1,2,3)
                 )

# name id
# 2 robert  2
# 3  peter  3