Error in regular expression

Hello. I have a long df with a character column in this format: A06005599. I want to filter all the ocurrences that begins with A24

I'm using

df[grepl("^A24.*\\\\\\", df)]

but gives me an error:

  Error in grepl("^A24.*\\\\\\", df) : 
  invalid regular expression, reason 'Trailing backslash'.

Any idea?

You can just use "^A24" as regular expression, if there are no further constraints. In addition, if you use grepl(pattern, df), greplwill not work properly. You should do df$my_colum inside the grepl() call:

df <- data.frame(
  strings = c('A06005599','A07005599','A24005599'),
  vals    = 1:3
  )

df[grepl('^A24', df$strings),]
#>     strings vals
#> 3 A24005599    3

Created on 2022-11-25 with reprex v2.0.2

Kind regards

A reprex would be helpful to diagnose the problem. My method would be

library(tidyverse)

df <- data.frame(x = c("A2105", "A2126", "A6167"),
                 y = c(12, 23, 34))
df |> filter(str_starts(x, "A21"))
#>       x  y
#> 1 A2105 12
#> 2 A2126 23

Created on 2022-11-25 with reprex v2.0.2

1 Like

Perfect. Thanks FactOREO.

This also works EconProf. Thanks a lot also.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.