Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.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_Algorithm_Subset - Fatal编程技术网

在r中搜索数据帧中字符串的快速方法

在r中搜索数据帧中字符串的快速方法,r,algorithm,subset,R,Algorithm,Subset,我有一个向量,它包含一些像这样的字符串 f <- c("a","b","c") 现在,我正在尝试编写代码,以找出包含与向量中相同字符串的行 i、 e:样本输出 h1 h2 ... 1 a 20 ... 2 a 50 ... 3 a 60 ... ... 我的解决方案是使用for循环遍历f中的每一项,然后使用grep查找我想要的行。并使用rbind()将这些行放在一起 for(item in f){ newdf <- rbind

我有一个向量,它包含一些像这样的字符串

f <- c("a","b","c")
现在,我正在尝试编写代码,以找出包含与向量中相同字符串的行

i、 e:样本输出

    h1  h2  ...
1   a   20  ...
2   a   50  ...
3   a   60  ...
  ...
我的解决方案是使用for循环遍历f中的每一项,然后使用grep查找我想要的行。并使用rbind()将这些行放在一起

for(item in f){
    newdf <- rbind(newdf, df[grep(item, df$h1),])
}
for(f中的项目){

newdf这应该比for循环快得多:

df[df$h1 %in% f,]
#  h1 h2
#1  a 20
#2  a 50
#3  a 60

您希望在%f中匹配
h1%
df[df$h1 %in% f,]
#  h1 h2
#1  a 20
#2  a 50
#3  a 60