gpt4 book ai didi

regex - 如何在R中仅grep 10场比赛?

转载 作者:行者123 更新时间:2023-12-01 00:49:35 26 4
gpt4 key购买 nike

我有一个数据文件,我可以在其中搜索“你”:

> 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com