Base R
поставляется с удобной для этого функцией, называемой list.files
. Используя аргумент шаблона, вы можете сузить область поиска ("."
дает вам все файлы). Используя all.files = TRUE
, вы также можете включить скрытые файлы.
folder <- "C:/Users/Johannes Gruber/Pictures"
files <- list.files(folder, pattern = ".", all.files = FALSE, recursive = TRUE, full.names = TRUE)
# number of all files
length(files)
#> [1] 182
Вы можете использовать split
, чтобы разбить этот вектор на список с содержимым каждой папки в отдельных элементах списка.
# 1. The number of files in each subfolder
dir_list <- split(files, dirname(files))
files_in_folder <- sapply(dir_list, length)
head(files_in_folder)
#> C:/Users/Pictures
#> 10
#> C:/Users/Pictures/2019/2019-12-30
#> 3
#> C:/Users/Pictures/2019/2019-12-31
#> 9
#> C:/Users/Pictures/2020/2020-01-01
#> 6
#> C:/Users/Pictures/2020/2020-01-03
#> 2
#> C:/Users/Pictures/2020/2020-01-04
#> 26
# 2. Whether that number is odd or even (but this is minor)
even <- sapply(files_in_folder, function(x) x %% 2 == 0)
head(even)
#> C:/Users/Pictures
#> TRUE
#> C:/Users/Pictures/2019/2019-12-30
#> FALSE
#> C:/Users/Pictures/2019/2019-12-31
#> FALSE
#> C:/Users/Pictures/2020/2020-01-01
#> TRUE
#> C:/Users/Pictures/2020/2020-01-03
#> TRUE
#> C:/Users/Pictures/2020/2020-01-04
#> TRUE