在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