[R] grep

Ista Zahn |@t@z@hn @end|ng |rom gm@||@com
Mon Jul 16 19:57:34 CEST 2018


grep("(^| )ABHD14A( ;|$)",xgen, value = TRUE)

maybe.

On Mon, Jul 16, 2018 at 1:46 PM, Brian Smith <bsmith030465 using gmail.com> wrote:
> Hi,
>
> I was trying to find a pattern ("ABHD14A") in a character string ('xgen' in
> example below) using grepl. Note that the individual members may be
> separated by a semi-colon.
>
> The correct answer should return:
>
> "ABHD-ACY1 ; ABHD14A" "ABHD14A ; YYY"
>
> I have tried three approaches, but still seem a bit off. Attempt 2 below
> gets closest, but it also returns a hit where my pattern is a substring.
> Here is my code:
>
> ===========
>
>
>   xgen <- c("XYZ","ABHD-ACY1 ; ABHD14A","ABHD14AXX","ABHD14A ; YYY")
>   ga <- "ABHD14A"
>
>   # 1.
>   kx <- grepl(paste0("^",ga,"$"),xgen)
>   xgen[kx]
>
>   # 2.
>   ky <- grepl(ga,xgen)
>   xgen[ky]
>
>
> ==============
>
> What do I need to add/change in #2 above?
>
> many thanks!
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list