Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 基于列中的部分字符串匹配选择数据帧行_R_Regex_String_Match_Subset - Fatal编程技术网

R 基于列中的部分字符串匹配选择数据帧行

R 基于列中的部分字符串匹配选择数据帧行,r,regex,string,match,subset,R,Regex,String,Match,Subset,我想根据列中字符串的部分匹配从数据框中选择行,例如,列“x”包含字符串“hsa”。使用sqldf-如果它有类似的语法-我会做如下操作: 从x中选择*如“hsa” 不幸的是,sqldf不支持这种语法 或类似地: selectedRows <- df[ , df$x %like% "hsa-"] selectedRows我注意到您在当前方法中提到了一个函数%like%。我不知道这是不是引用了“data.table”中的%like%,但如果是,您肯定可以按如下方式使用它 请

我想根据列中字符串的部分匹配从数据框中选择行,例如,列“x”包含字符串“hsa”。使用
sqldf
-如果它有类似
的语法-我会做如下操作:

从x中选择*如“hsa”

不幸的是,
sqldf
不支持这种语法

或类似地:

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

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

请注意,对象不必是
data.table
(但也请记住
data.frame
s和
data.table
s的子集设置方法不相同):

如果这就是您所拥有的,那么您可能只是混淆了子集数据的行和列位置


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

另一个例子是,使用
iris
数据集搜索字符串
osa

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

irisSubset
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
require(sqldf)
dfTry
str_detect()
从包中检测是否存在字符串中的模式

下面是一种方法,它还包含了包中的
%%>%%
管道和
过滤器()


这将过滤处理变量包含子字符串“non”的行的样本CO2数据集(与R一起提供)。您可以调整
str\u detect
是查找固定匹配项还是使用正则表达式-请参阅stringr包的文档。

另一个选项是只使用
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
df[grepl('er',df$name),]
二氧化碳[grepl('非',二氧化碳$处理),]

df您可以发布几行数据,最好使用类似于
dput(head(conservedData))
+1:还要注意
grep
支持正则表达式,因此您可能希望grep来代替
^hsa-
。@nico:事实上,
grep
来自ed命令g/re/p(全局/正则表达式/print),而它真正的力量只展现给正则表达式大师傅;-):%like%的建议太棒了!“我建议把它放在你答案的最上面。”阿伦坎布雷,完成了。也许这会帮助我再获得11张选票,这样我就可以在年底前得到一顶新帽子:-)@A5C1D2H2I1M1N2O1R2T1回答得很好!是否有一种方法可以使用%like%来搜索同时出现的两个字符串(如在数据帧的一行中出现的“pet”和“pip”中出现的“peter piper”)?SQLDF最适合列出。但是,它不能删除行。为什么R包在此处加载
require()
,因为它不是标准的R库,您必须手动安装它,然后使用
require
函数加载。您也可以像这样使用trc\u detect函数
myDataFrame[str\u detect(myDataFrame$key,myKeyPattern),]
selectedRows <- conservedData[grep("hsa-", conservedData$miRNA), ]
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
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
...
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