R function to filter a variable by the first letter

Viewed 3273

I have a variable that is alphanumeric - always starts with a letter and followed by numbers. Ex. A483, B472, K112

I want to create a dataset with only the variables that start with a certain letter. Ex. A dataset with only rows that included the variable starting with F.

I tried:

Fonlydata = data %>% filter(variable == "^[F]")

but doesn't work. Could anyone please help? Thank you

2 Answers

Your use of filter suggests that you are using dplyr. This is an example on how to use "normal" R with the buildin grep function on the names of a data frame:

expl <- data.frame(abs = rnorm(10), bcd = rnorm(10), foo = 1:10, def = rnorm(10), faa = 1:10)
print(expl)
result <- expl[,grep('^f', names(expl))]
print(result)

We can use substr to extract the first letter and then use ==

library(dplyr)
data %>% 
    filter(substr(variable, 1, 1) == "F")

Or another option is regex with str_detect

library(stringr)
data %>%
    filter(str_detect(variable, '^F'))
Related