Yann
Yann

Reputation: 957

Get the name of a list item created with purrr::map

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?

Upvotes: 13

Views: 6695

Answers (2)

David Klotz
David Klotz

Reputation: 2431

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)

Upvotes: 6

Spacedman
Spacedman

Reputation: 94172

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.

Upvotes: 22

Related Questions