[R] dplyr or plyr or both?

Christopher W. Ryan cryan at binghamton.edu
Thu Sep 15 20:32:57 CEST 2016


Thank you Frans. This is exactly the sort of nuance that I want to learn 
about.

--Chris

Frans Marcelissen wrote:
> Hello Christopher and others
> :
> What cannot be stressed enough is: do not combine both packages, it
> gives errors and incorrect results! I will show that below
> --------------------------------------------------------
> a<-data.frame(groep=1:4,v=1:40)
> library(dplyr)
> a %>% group_by(groep) %>% summarise(m=mean(v),n=n())
> # groep     m     n
> # <int> <dbl> <int>
> # 1     1    19    10
> # 2     2    20    10
> # 3     3    21    10
> # 4     4    22    10
> # correct
>
> library(plyr)
> a %>% group_by(groep) %>% summarise(m=mean(v),n=n())
>
> Error in n() : This function should not be called directly
> # ???
> a %>% group_by(groep) %>% summarise(m=mean(v))
> # m
> # 1 20.5
> #incorrect!
> --------------------------------------------------
>
> So both n() and group_by from dplyr don't work after library(plyr)!
>
> My advice is: do not use plyr. Unfortunately plyr has some functions
> that are very important, and that are not in dplyr. For
> instance: rbind.fill() (for combining the rows of two dataframes with
> unequal columns). If you need this: do'nt library plyr, use plyr::rbind.fil
>
> Until now I have the impression that it is also possible to library
> dplyr after plyr, but it is better to remove plyr!
>
> This is a serious problem that has been reported before, but not solved
> (in dplyr 0.5.0 and plyr 1.8.4)
>
> Frams
>
> 2016-09-15 16:09 GMT+02:00 Christopher W Ryan <cryan at binghamton.edu
> <mailto:cryan at binghamton.edu>>:
>
>     I've set myself the task of learning about these packages, and about
>     tidy data concepts.
>
>     What is the relationship between plyr and dplyr?  Does the latter
>     replace the former (meaning I can concentrate on learning the latter)?
>     Or is there ever a need to use functions from both (meaning I should
>     learn both)?
>
>     Thanks.
>
>     --Chris Ryan
>
>     ______________________________________________
>     R-help at r-project.org <mailto:R-help at r-project.org> mailing list --
>     To UNSUBSCRIBE and more, see
>     https://stat.ethz.ch/mailman/listinfo/r-help
>     <https://stat.ethz.ch/mailman/listinfo/r-help>
>     PLEASE do read the posting guide
>     http://www.R-project.org/posting-guide.html
>     <http://www.R-project.org/posting-guide.html>
>     and provide commented, minimal, self-contained, reproducible code.
>
>



More information about the R-help mailing list