R中因宏包冲突遇到的坑

dplyr与plyr的冲突

在做分组计算时,正常情况下使用dplyr就可以,如下:

library(dplyr)
zou=data.frame(n=rep(c(1,2),10),x=1:20)
zou%>%group_by(n)%>%summarise(z=mean(x))
# # A tibble: 2 x 2
#   n     z
# <dbl> <dbl>
#   1    10
#   2    11

但是,如果在把plyr添加进去,就是这样的:

library(dplyr)
library(plyr)
zou=data.frame(n=rep(c(1,2),10),x=1:20)
zou%>%group_by(n)%>%summarise(z=mean(x))
# z
# 1 10.5

原因是什么呢?R是有提示的,需要先加载plyr再加载dyplyr就可以了:

> library(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)
----------------------------------------------------------------------------------------

载入程辑包:‘plyr’

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

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

The following object is masked from ‘package:purrr’:

    compact
R中因宏包冲突遇到的坑R中因宏包冲突遇到的坑 zoujiahui_2018 发布了48 篇原创文章 · 获赞 24 · 访问量 8万+ 私信 关注
上一篇:Git—— ! [rejected] master -> master (fetch first)问题


下一篇:PDOStatement::fetchObject