I retrieved a list of csv files with purrr::map and got a large list.
csv_files <- list.files(path = data_path, pattern = '\\.csv$', full.names = TRUE)
all_csv <- purrr::map(csv_files, readr::read_csv2)
names(all_csv) <- gsub(data_path, "", csv_files)
return all_csv
EDITED as suggested by @Spacedman
I further need to process each tibble/data frame separately within the process_csv_data function.
purrr::map(all_csv, process_csv_data)
How to retrieve the name of a single item in the large list without for loop?
Use map2
, as in this reproducible example:
> L = list(a=1:10, b=1:5, c=1:6)
> map2(L, names(L), function(x,y){message("x is ",x," y is ",y)})
x is 12345678910 y is a
x is 12345 y is b
x is 123456 y is c
the output of the list as x
in the function gets a bit munged by message
, but its the list element of L
.
You can take advantage of purrr to keep all the data in a single, nested tibble. That way each csv and processed csv remains linked directly with the appropriate csv-name:
csv_files <- list.files(path = data_path, pattern = '\\.csv$', full.names = TRUE)
all_csv <- tibble(csv_files) %>%
mutate(data = map(csv_files, read_csv2),
processed = map(data, process_csv_data),
csv_files = gsub(data_path, "", csv_files)) %>%
select(-data)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With