purrr apply functions to lists in data.frame over specifed dimensions

蹲街弑〆低调 提交于 2019-12-11 17:53:29

问题


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.

来源:https://stackoverflow.com/questions/54471512/purrr-apply-functions-to-lists-in-data-frame-over-specifed-dimensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!