Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R function to filter a variable by the first letter

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

like image 200
Ryan S Avatar asked Jun 06 '26 05:06

Ryan S


2 Answers

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'))
like image 106
akrun Avatar answered Jun 10 '26 02:06

akrun


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)
like image 43
Bernhard Avatar answered Jun 10 '26 02:06

Bernhard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!