【问题标题】:group_by() and summarise() errorgroup_by() 和 summarise() 错误
【发布时间】:2018-12-24 16:43:18
【问题描述】:

我想通过函数 group_by() 和 summarise() 制作一个数据框,该数据框具有来自另一个数据框“pf”的friend_count 变量的平均值和中位数。

但是当我尝试这个命令时,结果如下图,显示了friend_count整列的均值和中位数,没有按年龄分组。

请帮忙!

library(dplyr)

pf.fc_by_age <- pf %>% group_by(age) %>%
              summarise(friend_count_mean = mean(friend_count),
                        friend_count_median = median(friend_count))
pf.fc_by_age
friend_count_mean friend_count_median
  1 196.3508 82

【问题讨论】:

  • 可能你也加载了plyr。使用%&gt;% dplyr::summarise(friend_count_mean=
  • 这么简单!!非常感谢:)
  • @Jean 这就是为什么有人建议使用语法package::function() 而不仅仅是function()。虽然您可能有其他错误,但这不会是因为包冲突/屏蔽;)

标签: r group-by dplyr


【解决方案1】:

我也有同样的问题。后来发现plyr这个包可能和tidyverse有冲突。当我只加载 tidyverse 并避免使用 plyr 时,这解决了我的问题。

【讨论】:

  • tidyverse 依赖于dplyr。没有 dplyr 就无法加载 tidyverse。
  • 哦,抱歉,是 plyr 包使用 rbind.fill() 导致了在我的情况下汇总平均值的问题。
猜你喜欢
  • 2022-11-15
  • 1970-01-01
  • 2019-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-27
  • 2020-03-13
相关资源
最近更新 更多