Home > Enterprise >  Get matched String in R using grepl
Get matched String in R using grepl

Time:12-15

I was trying to get matched strings using Grepl.

Code

dt_test <- data.frame(
                a = c("ab","abcd", "poo", "abla", "ba"),
                id = c(1,2,3, 4,5)
)
grepl('^ab|^ao', dt_test$a)

What it returns was TRUE TRUE FALSE TRUE FALSE But I want ro print Matched strings like "ab","abcd", "abla"

CodePudding user response:

Use grep with the value switch set to true:

grep('^a[bo]', dt_test$a, value=TRUE)

[1] "ab"   "abcd" "abla"
  • Related