A positive look at R - passing variables

I am stuck in regex. I usually use this line of code to find duplicate repetitions in lines:

gregexpr("(?=ATGGGCT)",text,perl=TRUE)
[[1]]
[1]  16  45  52  75 203 210 266 273 327 364 436 443 480 506 534 570 649
attr(,"match.length")
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
attr(,"useBytes")
[1] TRUE

Now I want to give the gregexprtemplate contained in the variable:

x="GGC"

and of course, if I pass in a variable x, it gregexprwill search "x", not what the variable contains

gregexpr("(?=x)",text,perl=TRUE)
[[1]]
[1] -1
attr(,"match.length")
[1] -1
attr(,"useBytes")
[1] TRUE

How can I pass my variable in gregexprin this case a positive look ahead?

+4
source share
4 answers

I would play with the function sprintf:

x <- "AGA"
text <- "ACAGAGACTTTAGATAGAGAAGA"
gregexpr(sprintf("(?=%s)", x), text, perl=TRUE)
## [[1]]
## [1]  3  5 12 16 18 21
## attr(,"match.length")
## [1] 0 0 0 0 0 0
## attr(,"useBytes")
## [1] TRUE

sprintfreplaces appearance %swith value x.

+5
source

paste0, paste(x, sep="")...

x <- "GGC"
text <- 'ATGGGCTATGGGCTATGGGCTATGGGCT'
gregexpr(paste0('(?=', x, ')'), text, perl=TRUE)
# [[1]]
# [1]  4 11 18 25
# attr(,"match.length")
# [1] 0 0 0 0
# attr(,"useBytes")
# [1] TRUE

, R

+4

fn$ gsubfn :

library(gsubfn)

# test data
text <- "ATGGGCTAAATGGGCT"
x <- "GGGC"

fn$gregexpr("(?=$x)", text, perl = TRUE)

. ?fn, gsubfn gsubfn, vignette("gsubfn").

+3

ok :

text="ATGGGCTAAATGGGCT"
x="GGC"
c=paste("(?=",x,")",sep="")
r=gregexpr(c,text,perl=TRUE)
0

All Articles