简体   繁体   中英

map_dfr outputting a row rather than a column

This is similar to purrr::map_dfr binds by columns, not row as expected but the solutions there aren't working for me. I have a dataframe like

beta_df <- structure(list(intercept = c(-2.75747056032685, -2.90831892599742, 
-2.92478082251453, -2.99701559041538, -2.88885796048347, -3.09564193631675
), B1 = c(0.0898235360814854, 0.0291839369781567, 0.0881023522236231, 
0.231703026085554, 0.0441573699433149, 0.258219673780526), B2 = c(-0.222367437619057, 
0.770536384299238, 0.199648657850609, 0.0529038155448773, 0.00310458335580774, 
0.132604387458483), B3 = c(1.26339268033385, 1.29883641278223, 
0.949504940387809, 1.26904511447941, 0.863882674439083, 0.823907268679309
), B4 = c(2.13662994525526, 1.02340744740827, 0.959079691725652, 
1.60672779812489, 1.19095838867883, -0.0693120654049908)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

#> # A tibble: 6 × 5
#>   intercept     B1       B2    B3      B4
#>       <dbl>  <dbl>    <dbl> <dbl>   <dbl>
#> 1     -2.76 0.0898 -0.222   1.26   2.14  
#> 2     -2.91 0.0292  0.771   1.30   1.02  
#> 3     -2.92 0.0881  0.200   0.950  0.959 
#> 4     -3.00 0.232   0.0529  1.27   1.61  
#> 5     -2.89 0.0442  0.00310 0.864  1.19  
#> 6     -3.10 0.258   0.133   0.824 -0.0693

I'd like to turn this into a tibble with columns for the mean, 0.025 and 0.975 quantiles. For the quantile function this works:

beta_df %>%
    map_dfr(quantile,0.025)

#> # A tibble: 5 × 1
#>    `2.5%`
#>     <dbl>
#> 1 -3.08  
#> 2  0.0311
#> 3 -0.194 
#> 4  0.829 
#> 5  0.0592

And this gets me both quantiles

bind_cols(beta_df %>%
    map_dfr(quantile, 0.025),
    beta_df %>%
    map_dfr(quantile, 0.975))

#> # A tibble: 5 × 2
#>    `2.5%` `97.5%`
#>     <dbl>   <dbl>
#> 1 -3.08    -2.77 
#> 2  0.0311   0.255
#> 3 -0.194    0.699
#> 4  0.829    1.30 
#> 5  0.0592   2.07

But for mean ,

beta_df %>%
    map_dfr(mean)

#> # A tibble: 1 × 5
#>   intercept    B1    B2    B3    B4
#>       <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1     -2.93 0.124 0.156  1.08  1.14

Gives me a long row rather than a column. How can I turn the mean of each column of the original dataframe into a row of a single column dataframe labelled mean?

The reason is because the output of quantile() is a named vector whereas for the mean() is just a single value.

Lets create a custom function with the mean that outputs a named vector,

myMean <- function(x) {setNames(mean(x), nm = 'theMean')}

Applying that using map_dfr we get,

library(dplyr)

beta_df %>%
    purrr::map_dfr(myMean)

# A tibble: 5 x 1
  theMean
    <dbl>
1  -2.93 
2   0.124
3   0.156
4   1.08 
5   1.14 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM