r语言 - 重写使用.groups参数



我一直收到"summarise()将输出按"new_brand"分组。您可以使用.groups参数。"我不确定我是否得到这个错误因为我创建了列pos_prop和neg_prop

superbowl %>% group_by(new_brand, superbowl) %>% summarize(mean(superbowl$volume, superbowl$pos_prop, superbowl$neg_prop), sd(superbowl$volume, superbowl$pos_prop, superbowl$neg_prop)) %>% filter(superbowl, superbowl == "0")

当我运行rlang::last_error()代码工作时,我不确定如何使代码正常运行。如有任何帮助,不胜感激。

您正在使用summarize等错误。试试这个:

superbowl %>%
group_by(new_brand) %>%
summarize(across(c(volume, pos_prop, neg_prop),
list(mu = ~ mean(.), sigma = ~ sd(.)))) %>%
filter(superbowl == "0")

代码注释:

  • 一旦你用superbowl %>%启动dplyr-管道,几乎永远不要在dplyr动词中使用superbowl$(非常罕见的例外);我还删除了group_byfilter中对superbowl的引用,因为不清楚您是否试图再次引用原始框架符号……如果你有superbowl$superbowl,那么它们可能仍然合适;
  • 可以像上面那样使用across(..),也可以将计算命名为summarize(volume_mu = mean(volume), pos_mu = mean(pos_prop), ...);和
  • 我在推断,但是…mean(volume, pos_prop, neg_prop)(有或没有superbowl$)是一个错误:在这种情况下,调用实际上是mean(volume, trim=pos_prop, na.rm=neg_prop),它应该产生错误。如果你真的想将三列的数据聚合成一个数字,一个可以将其适应为mean(c(volume, pos_prop, neg_prop)),但我认为这可能是无意的过度聚合。

用实际数据演示:

mtcars %>%
group_by(cyl) %>%
summarize(across(c(disp, mpg),
list(mu = ~ mean(.), sigma = ~ sd(.))))
# # A tibble: 3 x 5
#     cyl disp_mu disp_sigma mpg_mu mpg_sigma
#   <dbl>   <dbl>      <dbl>  <dbl>     <dbl>
# 1     4    105.       26.9   26.7      4.51
# 2     6    183.       41.6   19.7      1.45
# 3     8    353.       67.8   15.1      2.56

最新更新