We can see some good examples of how to filter a data.frame based on a substring; is there a tidy way of doing this for a vector? (that is, without using grepl()
or similar)
Example
I tried what would work on a data.frame
# Leave only words that don't begin with 'cat'
vec <- c("cat", "catamaran", "dog", "mouse", "catacombs")
vec %>% filter(substr(1, 3) != "cat") # %>% ... etc
but
Error in UseMethod("filter_") :
no applicable method for 'filter_' applied to an object of class "character"
Note
We could use something like vec %>% { .[!grepl("cat", .)] }
, or more accurately vec %>% { .[substr(., 1, 3) != "cat"]}
, but I will try to find something that..
- is more beginner friendly, with more verbally descriptive functions (e.g. a complete novice can probably guess what 'filter' does but possibly not 'grepl')
- has less finicky syntax (as few
{
and}
as possible) - pipes more elegantly (e.g.
vec %>% filter(...) %>% next operations
) - contains as little repetition as possible, noting that the
grepl
way uses the original vector (denoted by.
) twice (as opposed to just once which would be ideal)