dplyr:总结每列和返回列表列

dplyr:总结每列和返回列表列,r,dplyr,summarize,R,Dplyr,Summarize,我正在寻找一个自定义的摘要函数来总结一个TIBLE中的每一列,该函数将根据数据返回不同大小的TIBLE 假设我的摘要功能是: mysummary <- function(x) {quantile(x)[1:sample(1:5, 1)] %>% as_tibble} 但我无法通过总结所有内容(或类似内容)找到实现这一点的方法 使用cars数据,所需输出为: tribble( ~speed.summary, ~dist.summary, mysummary(cars$

我正在寻找一个自定义的摘要函数来总结一个TIBLE中的每一列,该函数将根据数据返回不同大小的TIBLE

假设我的摘要功能是:

mysummary <- function(x) {quantile(x)[1:sample(1:5, 1)] %>% as_tibble}
但我无法通过
总结所有内容(或类似内容)找到实现这一点的方法

使用
cars
数据,所需输出为:

tribble(
~speed.summary,        ~dist.summary, 
mysummary(cars$speed), mysummary(cars$dist)
)

# A tibble: 1 x 2
  speed.summary    dist.summary    
  <list>           <list>          
1 <tibble [5 x 1]> <tibble [2 x 1]>    
tribble(
~speed.summary,~dist.summary,
mysummary(汽车$speed),mysummary(汽车$dist)
)
#一个tibble:1x2
speed.summary dist.summary
1.
当然,实际数据有更多的列

建议?

我们可以使用

res <- cars %>%
        summarise_all(funs(summary = list(mysummary(.)))) %>% 
        as.tibble
res
# A tibble: 1 x 2
#   speed_summary    dist_summary    
#  <list>           <list>          
#1 <tibble [3 x 1]> <tibble [2 x 1]>

res$speed_summary
#[[1]]
# A tibble: 3 x 1
#   value
#* <dbl>
#1  4.00
#2 12.0 
#3 15.0 
res%
总结所有(funs)(总结=列表(我的总结)())%>%
如:泰布尔
物件
#一个tibble:1x2
#速度摘要区摘要
#                       
#1  
res$speed_摘要
#[[1]]
#一个tibble:3x1
#价值观
#* 
#1  4.00
#2 12.0 
#3 15.0 

这就是你的想法吗

#加载必要的库和数据
图书馆(tibble)
图书馆(purrr)
#>警告:包“purrr”是在R版本3.4.2下生成的
数据(汽车)
#自定义摘要函数(仅适用于数值变量)
我的总结价值
#> * 
#> 1  4.00
#> 2 12.0 
#> 3 15.0 
#> 4 19.0 
#> 5 25.0 
#> 
#>$dist
#>#tible:1 x 1
#>价值观
#> * 
#> 1  2.00

由(v0.1.1.9000)于2018年1月27日创建。

太棒了,正是我想要的!
res <- cars %>%
        summarise_all(funs(summary = list(mysummary(.)))) %>% 
        as.tibble
res
# A tibble: 1 x 2
#   speed_summary    dist_summary    
#  <list>           <list>          
#1 <tibble [3 x 1]> <tibble [2 x 1]>

res$speed_summary
#[[1]]
# A tibble: 3 x 1
#   value
#* <dbl>
#1  4.00
#2 12.0 
#3 15.0