Imagine a coauthor storms into your office one day and exclaims:
oh those survey firm dummies screwed up bad! I wanted our three country, three wave data in a nice long table–but instead, we got
`COUNTRYCODE_wave_WAVEN_.csv
`what dummies!
As the menschy coauthor you are, you don’t sit down and star writing
"wave_1/USA_wave_1.csv" %>% read_csv
"wave_1/ARG_wave_1.csv" %>% read_csv
…because that would be insane!
What’s a nice functional programming approach to address this problem?
library(tidyverse)
library(magrittr)
# 1. Set up a temporary directory
example_dir <- file.path(tempdir(), "example_project")
dir.create(example_dir, recursive = TRUE)
# 2. Create three subdirectories using purrr::map
sub_dirs <- c("subdir1", "subdir2", "subdir3")
map(sub_dirs, ~ dir.create(file.path(example_dir, .x)))
# 3. Define toy tibbles
toy_tibbles <- list(
tibble(a = 1, b = "x", c = 1.1, d = TRUE),
tibble(a = 2, b = "y", c = 2.2, d = FALSE),
tibble(a = 3, b = "z", c = 3.3, d = TRUE),
tibble(a = 4, b = "w", c = 4.4, d = FALSE),
tibble(a = 5, b = "v", c = 5.5, d = TRUE),
tibble(a = 6, b = "u", c = 6.6, d = TRUE),
tibble(a = 7, b = "r", c = 7.7, d = TRUE),
tibble(a = 8, b = "s", c = 8.8, d = TRUE),
tibble(a = 9, b = "t", c = 9.9, d = FALSE)
)
# 4. Define consistent file names
file_names <- str_c(
c("USA", "ARG", "SLE") %>%
rep(times = 3),
"_wave_",
1:3 %>%
rep(each = 3),
".csv"
)
# 5. Save tibbles to each subdirectory using purrr::walk2
map2(sub_dirs, split(toy_tibbles, rep(1:3, each = 3)), ~ {
sub_dir_path <- file.path(example_dir, .x)
walk2(.y, file_names[((which(sub_dirs == .x) - 1) * 3 + 1):((which(sub_dirs == .x)) * 3)],
~ write_csv(.x, file.path(sub_dir_path, .y)))
})
list.files(example_dir, recursive = TRUE)