Forgive the lack of REPREX, but I have a data frame, df
How do I ask if the data frame contains a rowname that has the first 3 letters "ABC"?
Thanks!
Forgive the lack of REPREX, but I have a data frame, df
How do I ask if the data frame contains a rowname that has the first 3 letters "ABC"?
Thanks!
Here are three versions of detecting ABC at the beginning of the row names using grep functions from base R. The ^ before the ABC signifies the beginning of the string so that the first row name does not match.
DF <- data.frame(Value = 1:5, row.names = c("WABC", "TREWS", "ABCand", "UIO", "BDDSE"))
grep(pattern = "^ABC", row.names(DF))
#> [1] 3
grepl(pattern = "^ABC", row.names(DF))
#> [1] FALSE FALSE TRUE FALSE FALSE
any(grepl(pattern = "^ABC", row.names(DF)))
#> [1] TRUE
Created on 2020-09-14 by the reprex package (v0.3.0)
beautiful, thanks so much
Here's a cheat sheet for regular expressions in R that I find super helpful--I've been writing regex for years and still refer back to this all. the. time.
Regular expressions are bewildering; there is only one rule that I remember by heart (for others there are cheatsheets): if you start with power (^
), you end up with money ($
)
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.