简体   繁体   English

Tidyverse 分组 function 进行总结?

[英]Tidyverse groups function for summarize?

I noticed that when using the group_by statement with summarize, I get a warning that the 'regrouping is being overridden by the.groups argument'.我注意到,当使用带有 summarise 的 group_by 语句时,我收到一个警告,指出“重新分组正在被 .groups 参数覆盖”。 I found one article online that seems to indicate that a group_by statement is no longer necessary -- one just needs to include a group argument with summarize.我在网上找到了一篇文章,似乎表明不再需要 group_by 语句——只需在汇总中包含一个 group 参数即可。 I would like to figure out how to make this work as I'm prepping an online tutorial for students, and the less code the better.在为学生准备在线教程时,我想弄清楚如何完成这项工作,并且代码越少越好。 But for me, it's not working.但对我来说,它不起作用。 Below is my Reprex.下面是我的代表。 I'm just trying to get the mean age of each gender我只是想得到每个性别的平均年龄

library(tidyverse)

femaledata <- data.frame(age = rnorm(n=5, mean = 29, sd = 4), gender = "female")
maledata <- data.frame(age = rnorm(n=5, mean = 37, sd = 6), gender = "male")
alldata <- bind_rows(femaledata, maledata)

summarydata <- alldata %>%
  summarize(gender, meanage = mean(age))

The summarydata dataframe should just have two rows (one for female and one for male) with the mean age for each.摘要数据 dataframe 应该只有两行(女性一行,男性一行),每行的平均年龄。 Instead my dataframe looks like this:相反,我的 dataframe 看起来像这样:

gender   meanage
female    32.6
female    32.6
female    32.6
female    32.6
female    32.6
male      32.6
male      32.6
male      32.6
male      32.6
male      32.6

I know the group_by statement isn't complicated, but if I could get rid of a line of code, all the better.我知道 group_by 语句并不复杂,但如果我能去掉一行代码,那就更好了。

Thanks, Wythe谢谢, 威斯

The new.group option is not to replace the group_by function. new.group 选项不是替换 group_by function。 The option is to let you set the way grouping should be handled.该选项是让您设置应该处理分组的方式。

  • "drop_last": dropping the last level of grouping. “drop_last”:删除最后一级分组。 This was the only supported option before version 1.0.0.这是 1.0.0 版之前唯一受支持的选项。
  • "drop": All levels of grouping are dropped. “drop”:所有级别的分组都被删除。
  • "keep": Same grouping structure as.data. “keep”:与.data 相同的分组结构。
  • "rowwise": Each row is it's own group. “rowwise”:每一行都是它自己的组。

The warning message when no.group option is set can be turned off by setting dplyr.summarise.inform to false.设置 no.group 选项时的警告消息可以通过将 dplyr.summarise.inform 设置为 false 来关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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