purrr apply functions to lists in data.frame over

2019-08-31 01:11发布

问题:

The following produces a data frame with <int[]> and <list[]> fields:

library(tidyverse)
set.seed(123)

s <- 4
data <- data.frame(
    lamda = c(5, 2, 3), 
    meanlog = c(9, 10, 11), 
    sdlog = c(2, 2.1, 2.2)
)

data2 <- data %>%
    mutate(
        freq = map(lamda, ~rpois(s, .x)),
        freqsev = map(freq, ~map(.x, function(k) rlnorm(k, meanlog, sdlog)))
    )

Output: as_tibble(data2)

lamda meanlog sdlog freq      freqsev   
<dbl>   <dbl> <dbl> <list>    <list>    
1     5       9   2   <int [4]> <list [4]>
2     2      10   2.1 <int [4]> <list [4]>
3     3      11   2.2 <int [4]> <list [4]>

I would like to create new fields with the mean of freq (producing a double), averaging over simulation s, and the sum of freqsev (producing <dbl [4]> where the [4] is the index of s) i.e. we sum over the number of occurrences e.g.

For data2$freq[[1]] I would expect the mean.

For data2$freqsev[[1]][[1]] I would expect the sum.

Additionally, I would like to create a field or preferably a handful of fields based on the the percentile function applied to freq; something like:

quantile(data2$freq[[1]], c(0.5, 0.75, 0.9))

producing fields quantile_0.5, quantile_0.75, quantile_0.9 of <dbl>

I have tried the purrr map function but I don't know how to choose the specific dimension to apply the operation over. Thank you.