How to grep a vector and return a single TRUE or FALSE?

前端 未结 4 1659
醉梦人生
醉梦人生 2020-12-14 21:25

Is there a grep function in R that returns TRUE if a pattern is found anywhere in the given character vector and FALSE otherwise?

4条回答
  •  误落风尘
    2020-12-14 21:55

    Perhaps you're looking for grepl()?

    > grepl("is", c("This", "is", "a", "test", "isn't", "it?"))
    [1]  TRUE  TRUE FALSE FALSE  TRUE FALSE
    

    Where the first argument is the pattern you're looking for, the second argument is the vector against which you want to match, and the returned value is a Boolean vector of the same length describing whether or not the pattern was matched to each element.

提交回复
热议问题