I have the following vector of directories.
c("", "Dia 04", "Dia 04/Basler_2020-11-04 13_08_41.568967_bg",
"Dia 04/Basler_2020-11-04 13_08_41.568967_frames", "Dia 04/Basler_2020-11-04 13_38_46.380957_bg",
"Dia 04/Basler_2020-11-04 13_38_46.380957_frames", "Dia 04/Basler_2020-11-04 14_08_51.133352_bg",
"Dia 04/Basler_2020-11-04 14_08_51.133352_frames", "Dia 04/Basler_2020-11-04 14_38_55.875411_bg",
"Dia 04/Basler_2020-11-04 14_38_55.875411_frames")
I would like to get the unique
names that start after the name "Basler_" until the name "_bg" or "_frames".
I try to use pattern
in list.dirs()
but wont works list.dirs(path = ".", full.names = F, recursive = TRUE, pattern = '(?<=/Basler_).*(?=_bg)')
output desired:
c("2020-11-04 13_08_41.568967", "2020-11-04 13_38_46.380957", "Basler_2020-11-04 14_08_51.133352", "2020-11-04 14_38_55.875411")
Thanks all
CodePudding user response:
Given this data:
names <- c("", "Dia 04", "Dia 04/Basler_2020-11-04 13_08_41.568967_bg",
"Dia 04/Basler_2020-11-04 13_08_41.568967_frames", "Dia 04/Basler_2020-11-04 13_38_46.380957_bg",
"Dia 04/Basler_2020-11-04 13_38_46.380957_frames", "Dia 04/Basler_2020-11-04 14_08_51.133352_bg",
"Dia 04/Basler_2020-11-04 14_08_51.133352_frames", "Dia 04/Basler_2020-11-04 14_38_55.875411_bg",
"Dia 04/Basler_2020-11-04 14_38_55.875411_frames")
You can use str_extract
:
base R
:
unique(str_extract(names, "(?<=Basler_)[\\d_. -] (?=_bg)"))
tidyverse
:
library(tidyverse)
data.frame(names) %>%
mutate(name_0 = str_extract(names, "(?<=Basler_)[\\d_. -] (?=_bg)")) %>%
pull(name_0) %>%
unique(.)
[1] NA "2020-11-04 13_08_41.568967" "2020-11-04 13_38_46.380957" "2020-11-04 14_08_51.133352"
[5] "2020-11-04 14_38_55.875411"