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

Time:11-30

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

  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.

CodePudding user response:

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.)

  • Related