使用.groups参数覆盖

发布于 2025-01-22 03:47:38 字数 454 浏览 4 评论 0原文

我不断获得“ summarize()已按'new_brand'分组输出。您可以使用 .groups参数。“我不确定我是否会遇到此错误,因为我

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() code时 创建了pos_prop和neg_prop有效,我不确定如何正确运行代码。

I keep getting "summarise() has grouped output by 'new_brand'. You can override using
the .groups argument." I'm not sure if I'm getting this error because I created columns pos_prop and 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")

When I run rlang::last_error() The code works, I'm not sure how to make the code run properly. Any help will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心头的小情儿 2025-01-29 03:47:38

您正在使用总结和此类错误。尝试以下尝试:

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

在您的代码上注释:

  • 启动dplyr -pipe,带有superbowl%>%,几乎从不$ 在dplyr动词中(非常罕见的例外);我还删除了superbowl的引用,group_byfilter ,因为尚不清楚您是否要参考原始帧符号同样...如果您有superbowl $ superbowl,那么它们仍然可能是合适的;
  • 要么使用跨(..)如上所述,要么命名计算,例如,summarize(polumion_mu = sean = polums_mu = pos_mu = mean(pos_prop),...)> ;和
  • 我正在推断,但是... 平均值(卷,pos_prop,neg_prop)(有或没有superbowl $)是一个错误:在这种情况下,呼叫有效均值(卷,trim = pos_prop,na.rm = neg_prop),应该产生错误。一个 可以将其适应为平均值(c(卷,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

You're using summarize and such incorrectly. Try this:

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

Notes on your code:

  • once you start a dplyr-pipe with superbowl %>%, almost never use superbowl$ in the dplyr verbs (very rare exceptions); I also removed references to superbowl in both group_by and filter, since it is not clear if you're trying to refer to the original frame symbol again ... if you have superbowl$superbowl, then they may still be appropriate;
  • either use across(..) as above or name the calculations, e.g., summarize(volume_mu = mean(volume), pos_mu = mean(pos_prop), ...); and
  • I'm inferring, but ... mean(volume, pos_prop, neg_prop) (with or without the superbowl$) is an error: in this case, the call is effectively mean(volume, trim=pos_prop, na.rm=neg_prop), which should be producing errors. One could adapt this to be mean(c(volume, pos_prop, neg_prop)) if you really want to aggregate three columns' data into a single number, but I thought that might be unintended over-aggregation.

Demonstration of this with real data:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文