adl
adl

Reputation: 1441

r - apply a function on each nested dataframe that is within a dataframe within a list

I have a structure on which I want to apply a function, but cannot get it right using purrr::map.

There are two nested dataframes within a list. Function needs to be applied on all of the elements of the nested dataframes. To reproduce the data structure:

df1 <- data.frame(a = c(1,1,2,2,3,3),
              b = c(1,2,3,4,5,6))
df1 <- df1 %>% 
    group_by(a) %>% 
    nest()

df2 <- data.frame(m = c(1,1,1,2,3,3),
                  n = c(6:11)) 
df2 <- df2 %>% 
    group_by(m) %>% 
    nest()

ls1 <- list(df1,df2)

Simple function like mean or max can be used:

f1 <- function(x) {
    x %>% 
        unnest() %>% 
        summarise(b = sum(b))
}

ls2 <- ls1 %>% map(~ .x, f1)

this doesn't manage to do the job. Ideas to solve this with "purrr" are ideal but any is welcome.

Upvotes: 3

Views: 455

Answers (1)

RLave
RLave

Reputation: 8364

I don't know if this is the best solution, but it should do the job:

library(purrr)

map(ls1, function(x) {
  map(x, mean)
})

# [[1]]
# [[1]]$a
# [1] 2
# 
# [[1]]$b
# [1] 3.5
# 
# 
# [[2]]
# [[2]]$m
# [1] 1.833333
# 
# [[2]]$n
# [1] 8.5

Basically I nested two map as you can see. Remember that purrr gives you the ability to better control the output with some variants like map_df or map_dbl, unlike some *apply.

Upvotes: 3

Related Questions