Here is a tidy and efficient way for data.frames:
library(dplyr)
df %>%
mutate(Lines = stringr::str_extract_all(Lines, "L\\d*([NSEW]?)")) %>%
tidyr::unnest(Lines)
#> # A tibble: 11 × 3
#> Type Lines Year
#> <chr> <chr> <dbl>
#> 1 METRO L5 1959
#> 2 METRO L5 1959
#> 3 METRO L5 1959
#> 4 METRO L9N 2009
#> 5 METRO L10N 2009
#> 6 METRO L9S 2016
#> 7 METRO L10S 2018
#> 8 METRO L10N 2010
#> 9 METRO L4 1926
#> 10 METRO L1 1926
#> 11 METRO L1 1926
Created on 2022-04-01 by the reprex package (v2.0.1)
It will work for any duplicated line following the pattern: L <som number> <possible one of N, S, E or W>
.
Data
df <- tibble::tribble(
~Type, ~Lines, ~Year,
"METRO", "L5", 1959,
"METRO", "L5", 1959,
"METRO", "L5", 1959,
"METRO", "L9NL10N", 2009,
"METRO", "L9S", 2016,
"METRO", "L10S", 2018,
"METRO", "L10N", 2010,
"METRO", "L4", 1926,
"METRO", "L1", 1926,
"METRO", "L1", 1926
)