I've got a question with the map function from the Purrr package.
- I can successfully pass on a list of data frames to a function using map
- the output remains a list and that's my issue ; I need to have the modified data frames as R objects
As an example with the mtcars dataset:
#I create a second df
mtcars2 <- mtcars
#change one variable just to distinguish them
mtcars2$mpg <- mtcars2$mpg / 2
#create the list
dflist <- list(mtcars,mtcars2)
#then, a simple function example
my_fun <- function(x)
{x <- x %>%
summarise(`sum of mpg` = sum(mpg),
`sum of cyl` = sum(cyl)
)
}
#then, using map, this works and prints the desired results
list_results <- map(dflist,my_fun)
But, I would need to have the modified mtcars and mtcars2 saved as r objects (dataframes).
- Should I add a "save" option of some kind to my function ?
- Should I use map_df or dmap ? (My trials were unsuccessful)
In advance, thanks a lot to all of you !
list. But, if you need to have multiple objects in global env, then check?list2env- akrunmap_df(dflist, my_fun)not work? - CPak