Parsing Lane Direction from Device Names of Varying Lengths

Hi, welcome!

You can use regular expressions, like this

library(stringr)
library(dplyr)

sample_data <- data.frame(stringsAsFactors = FALSE,
                 device_name = c("US54 @ IKE Pkwy-West - EB - L1",
                                 "Airport North - NB - L1",
                                 "US54 at Dugan-WB-L1",
                                 "K-96 at 21st - SB - L1"))

sample_data %>% 
    mutate(direction = str_extract(device_name, "[:alpha:]{2}(?=\\s?-\\s?[:alnum:]{2}$)"))
#>                      device_name direction
#> 1 US54 @ IKE Pkwy-West - EB - L1        EB
#> 2        Airport North - NB - L1        NB
#> 3            US54 at Dugan-WB-L1        WB
#> 4         K-96 at 21st - SB - L1        SB

Created on 2019-10-17 by the reprex package (v0.3.0.9000)

Note: Next time please make your questions with a REPRoducible EXample (reprex) like the one above.

1 Like