Reputation: 2105
I have got a list of dataframes where I want to perform some data wrangling operations on. For every year, I got a new list of data.frames
results_2018 <- list_of_objects %>%
map(~dplyr::top_n(.x, 10, Germany)) %>%
map(~rename(.x, "Answers" = "Answer.Options"))
results_2019 <- list_of_objects_2 %>%
map(~dplyr::top_n(.x, 10, Germany)) %>%
map(~rename(.x, "Answers" = "Data.Points"))
This is my code where I calculate the top 10 values for each year for one country. Since there are 10 years in history, is there a way to comebine these calculations into a single function?
I guess map2
and pmap
might do the job, but I can´t get my head around how this works.
Can anyone help me? (sorry for not providing reproduceable data, datasets are quite large)
Upvotes: 0
Views: 33
Reputation: 389355
You can try :
library(tidyverse)
list_obj <- list(list_of_objects, list_of_objects_2, ..., ..)
#If there are lot of them use
#list_obj <- mget(ls(pattern = 'list_of_objects\\d+'))
output <- map(list_obj, ~map(.x, function(x)
x %>% top_n(10, Germany) %>% rename("Answers" = "Answer.Options"))
This would return you list of lists, possibly using map_df
for inner map
would be useful.
map(list_obj, ~map_df(.x, function(x)
x %>% top_n(10, Germany) %>% rename("Answers" = "Answer.Options"))
Upvotes: 1