I'd like to iterate over a series of dataframes and apply the same function to them all.
I'm trying this using tidyr::nest and purrr::map_df. Here's a reprex of the sort of thing I'm trying to achieve.
data(iris)
library(purrr)
library(tidyr)
iris_df <- as.data.frame(iris)
my_var <- 2
my_fun <- function(df) {
sum_df <- sum(df) + my_var
}
iris_df %>% group_by(Species) %>% nest() %>% map_df(.$data, my_fun)
# Error: Index 1 must have length 1
What am I doing wrong? Is there a different approach?
EDIT: To clarify my desired output. Aiming for new column containing output eg
|Species|Data|my_function_output|
|:------|:---|:-----------------|
|setosa |<tibble>|509.1 |
nest(), it actually creates alistcolumn in your 'parent' data.frame (i.e.,iris). To do what you want, you need to combinemutatewithmaplike so:%>% mutate(data = map(data, ~my_fun))- CPakiris_df %>% group_by(Species) %>% nest() %>% mutate(my_col = map_df(data, ~my_fun)). Returns#Error in mutate_impl(.data, dots) : Evaluation error: Argument 1 must be a data frame or a named atomic vector, not a function.- markmap_dfin error butmapdoesn't give correct output.iris_df %>% group_by(Species) %>% nest() %>% mutate(my_col = map_dbl(data, my_fun))as per @Renu gives output needed. - markmap_dblworks as you've pointed out because you're returning a numeric value - CPak