为什么当我在`dplyr`之后加载`plyr`时,总结或变异不能与group_by一起使用? [英] Why does summarize or mutate not work with group_by when I load `plyr` after `dplyr`?

查看:225
本文介绍了为什么当我在`dplyr`之后加载`plyr`时,总结或变异不能与group_by一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:该问题的标题经过编辑,使其成为当plyr函数掩盖其dplyr对应物时出现的问题的规范问题.其余问题保持不变.

Note: The title of this question has been edited to make it the canonical question for issues when plyr functions mask their dplyr counterparts. The rest of the question remains unchanged.

假设我有以下数据:

dfx <- data.frame(
  group = c(rep('A', 8), rep('B', 15), rep('C', 6)),
  sex = sample(c("M", "F"), size = 29, replace = TRUE),
  age = runif(n = 29, min = 18, max = 54)
)

使用良好的旧版plyr,我可以使用以下代码创建一个小表来汇总我的数据:

With the good old plyr I can create a little table summarizing my data with the following code:

require(plyr)
ddply(dfx, .(group, sex), summarize,
      mean = round(mean(age), 2),
      sd = round(sd(age), 2))

输出看起来像这样:

  group sex  mean    sd
1     A   F 49.68  5.68
2     A   M 32.21  6.27
3     B   F 31.87  9.80
4     B   M 37.54  9.73
5     C   F 40.61 15.21
6     C   M 36.33 11.33

我正在尝试将代码移至dplyr%>%运算符.我的代码使用DF,然后按组和性别对其进行分组,然后对其进行汇总.那就是:

I'm trying to move my code to dplyr and the %>% operator. My code takes DF then group it by group and sex and then summarise it. That is:

dfx %>% group_by(group, sex) %>% 
  summarise(mean = round(mean(age), 2), sd = round(sd(age), 2))

但是我的输出是:

  mean   sd
1 35.56 9.92

我在做什么错了?

推荐答案

此处的问题是您先加载dplyr,然后再加载plyr,因此plyr的函数summarise掩盖了dplyr的函数summarise.发生这种情况时,您会收到以下警告:

The problem here is that you are loading dplyr first and then plyr, so plyr's function summarise is masking dplyr's function summarise. When that happens you get this warning:

require(plyr)
    Loading required package: plyr
------------------------------------------------------------------------------------------
You have loaded plyr after dplyr - this is likely to cause problems.
If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
library(plyr); library(dplyr)
------------------------------------------------------------------------------------------

Attaching package: ‘plyr’

The following objects are masked from ‘package:dplyr’:

    arrange, desc, failwith, id, mutate, summarise, summarize

因此,为了使您的代码正常工作,请分离plyr detach(package:plyr)或重新启动R并先加载plyr,然后再加载dplyr(或仅加载dplyr):

So in order for your code to work, either detach plyr detach(package:plyr) or restart R and load plyr first and then dplyr (or load only dplyr):

library(dplyr)
dfx %>% group_by(group, sex) %>% 
  summarise(mean = round(mean(age), 2), sd = round(sd(age), 2))
Source: local data frame [6 x 4]
Groups: group

  group sex  mean    sd
1     A   F 41.51  8.24
2     A   M 32.23 11.85
3     B   F 38.79 11.93
4     B   M 31.00  7.92
5     C   F 24.97  7.46
6     C   M 36.17  9.11

或者您可以在代码中显式调用dplyr的summary,因此无论您如何加载软件包,都将调用正确的函数:

Or you can explicitly call dplyr's summarise in your code, so the right function will be called no matter how you load the packages:

dfx %>% group_by(group, sex) %>% 
  dplyr::summarise(mean = round(mean(age), 2), sd = round(sd(age), 2))

这篇关于为什么当我在`dplyr`之后加载`plyr`时,总结或变异不能与group_by一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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