Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filter all rows that start with any Latin alphabetic letter in R

How can I filter all rows that start with any Latin alphabetic letter in R

sample code that is not working

library(dplyr)

df <- data.frame( marks = c(20.1, 30.2, 40.3, 50.4, 60.5),
                  
                  age = c(21:25),
                  
                  roles = c('Software Eng.', 'Software Dev', 
                            'Data Analyst', 'Data Eng.',
                            '5Sigma'))

df %>% filter(grep("[A-z]", roles))

Desired output

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  marks age         roles
1  20.1  21 Software Eng.
2  30.2  22  Software Dev
3  40.3  23  Data Analyst
4  50.4  24     Data Eng.

>Solution :

First, [A-z] is not the same as [A-Za-z], you need to be more careful with character classes. (See Difference between regex [A-z] and [a-zA-Z] and ignore the portions.)

Second, where does field: come in? Do this:

df %>%
  filter(grepl("^[A-Za-z]", roles))
#   marks age         roles
# 1  20.1  21 Software Eng.
# 2  30.2  22  Software Dev
# 3  40.3  23  Data Analyst
# 4  50.4  24     Data Eng.

(Plus the previous comment about grepl versus grep.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading