Поскольку это имена файлов, вы также можете использовать пакет fs
, чтобы проверить, имеет ли файл определенный родительский элемент, и позволить fs
иметь дело с разделителями файлов.
library("tidyverse")
files <- c(
"D:\\Data\\this\\way\\test\\dat1",
"D:\\Data\\this\\way\\test\\dat2",
"D:\\Data\\not-this\\way\\test\\dat1",
"D:\\Data\\not-this\\way\\test\\dat2"
)
tibble(
file = files
) %>%
filter(map_lgl(file, ~ fs::path_has_parent(., "D:/Data/this/way")))
#> # A tibble: 2 x 1
#> file
#> <chr>
#> 1 "D:\\Data\\this\\way\\test\\dat1"
#> 2 "D:\\Data\\this\\way\\test\\dat2"
# Explanation:
# The `map_lgl` applies `fs::path_has_parent` to each file
# and returns TRUE/FALSE (logical = lgl) values.
# Without `map`:
fs::path_has_parent(files, "D:/Data/this/way")
#> [1] FALSE
# With `map`:
map_lgl(files, ~ fs::path_has_parent(., "D:/Data/this/way"))
#> [1] TRUE TRUE FALSE FALSE
# The `~` operator creates a formula.
# Here it is shorter than defining an inline function.
# Formula:
map_lgl(files, ~ fs::path_has_parent(., "D:/Data/this/way"))
#> [1] TRUE TRUE FALSE FALSE
# Function:
map_lgl(files, function(x) fs::path_has_parent(x, "D:/Data/this/way"))
#> [1] TRUE TRUE FALSE FALSE
Созданона 2019-11-02 пакетом представьте (v0.3.0)