Parsing Lane Direction from Device Names of Varying Lengths

I have traffic sensor data that measures traffic flow and each device is given a device name. I need to be able to parse out the direction of travel so I can aggregate the flow directions. A sample of the different names is below. I want to create a new column with the NB, SB, EB & WB information. I appreciate any help with this problem.

US54 @ IKE Pkwy-West - EB - L1
Airport North - NB - L1
US54 at Dugan-WB-L1
K-96 at 21st - SB - L1

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

Awesome. Thank you very much and I will be sure and use a reprex next time.

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