作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个数据文件,我可以在其中搜索“你”:
> dtq_ml_wuv[grep("you", dtq_ml_wuv$rn), "rn"]
"you can take" "you can tell" "you can thank"
"you can try" "you can turn" "you can use"
"you can visit" "you can work" "you donet know"
"you donet need" "you dont know" "you get enough"
"you get see" "you go back" "you got keep"
"you guys can" "you heard right" "you just go"
"you just gotta" "you just look" "you just need"
"you just stay" "you know better" "you know else"
"you know got" "you know i" "you know if"
"you know im" "you know it" "you know just"
"you know many" "you know means" "you know one"
"you know really" "you know right" "you like see"
grep
至
停止找到后,说 0 到 25 个匹配项?
> dtq_ml_wuv[grep("you{0, 25}", dtq_ml_wuv$rn), "rn"]
{}
的内容无效.
最佳答案
这是一个分块版本。调整块大小以充分利用内部编译代码的速度,而不是搜索太多。
grepn<-function(pattern,x,n,chunk.size=32) {
N<-length(x);
chunk<-1:chunk.size;
k<-1;
M<-vector("integer",n+chunk.size);
while(k < n && chunk[1] <= N) {
i<-na.omit(grep(pattern,x[chunk]));
if(length(i)) M[k:(k+length(i)-1)]<-i+chunk[1]-1
k<-k+length(i)
chunk<-chunk+chunk.size
}
return(M[1:(min(k-1,n))])
}
data
作为问题中的字符向量
grepn("e",data,n=10,chunk.size=16)
[1] 1 2 6 9 10 12 13 15 17 21
关于regex - 如何在R中仅grep 10场比赛?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31990794/
我是一名优秀的程序员,十分优秀!