其他分享
首页 > 其他分享> > R中因宏包冲突遇到的坑

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
zoujiahui_2018 发布了48 篇原创文章 · 获赞 24 · 访问量 8万+ 私信 关注

标签:10,zou%,dplyr,遇到,因宏包,library,plyr,冲突,summarise
来源: https://blog.csdn.net/qq_18055167/article/details/104391279