汇总到矢量输出 [英] summarize to vector output

查看:51
本文介绍了汇总到矢量输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下(简化的)小标题,其中包含一组和向量中的值:

Let's say I have the following (simplified) tibble containing a group and values in vectors:

set.seed(1)
(tb_vec <- tibble(group = factor(rep(c("A","B"), c(2,3))),
             values = replicate(5, sample(3), simplify = FALSE)))
# A tibble: 5 x 2
  group values   
  <fct> <list>   
1 A     <int [3]>
2 A     <int [3]>
3 B     <int [3]>
4 B     <int [3]>
5 B     <int [3]>

tb_vec[[1,2]]
[1] 1 3 2

我想通过将每个值向量相加(向量化)来总结它们,并尝试以下操作:

I would like to summarize the values vectors per group by summing them (vectorized) and tried the following:

tb_vec %>% group_by(group) %>% 
  summarize(vec_sum = colSums(purrr::reduce(values, rbind)))

错误:列 vec_sum 的长度必须为1(摘要值),而不是3

Error: Column vec_sum must be length 1 (a summary value), not 3

该错误使我感到惊讶,因为小标题(输出格式)也可以包含矢量.

The error surprises me, because tibbles (the output format) can contain vectors as well.

我的预期输出将是以下摘要:

My expected output would be the following summarized tibble:

# A tibble: 2 x 2
  group vec_sum  
  <fct> <list>   
1 A     <dbl [3]>
2 B     <dbl [3]>

是否有一个tidyverse解决方案可以容纳summary的向量输出?我要避免分裂小标题,因为那样的话我就放松了这个因素.

Is there a tidyverse solution accommodate the vector output of summarize? I want to avoid splitting the tibble, because then I loose the factor.

推荐答案

您只需在解决方案的 summary 中添加 list(.)能够包含2个元素的列,其中每个元素是3个值的向量:

You just need to add list(.) within summarise in your solution, in order to be able to have a column with 2 elements, where each element is a vector of 3 values:

library(tidyverse)

set.seed(1)
(tb_vec <- tibble(group = factor(rep(c("A","B"), c(2,3))),
                  values = replicate(5, sample(3), simplify = FALSE)))

tb_vec %>% 
  group_by(group) %>%                              
  summarize(vec_sum = list(colSums(purrr::reduce(values, rbind)))) -> res

res$vec_sum

# [[1]]
# [1] 2 4 6
# 
# [[2]]
# [1] 6 5 7

这篇关于汇总到矢量输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆