understanding ddply error message

点点圈 提交于 2019-12-03 12:32:25

As stated in Narendra's comment to the question, this error can be caused by loading other packages that have a function called summarize (or summarise) that does not work as the function in plyr. For instance:

library(plyr)
library(Hmisc)

ddply(iris, "Species", summarize, mean_sepal_length = mean(Sepal.Length))
#> Error in .fun(piece, ...) : argument "by" is missing, with no default

One solution is to call the correct function with :: and the correct namespace:

ddply(iris, "Species", plyr::summarize, mean_sepal_length = mean(Sepal.Length))
#> Species mean_sepal_length
#> 1     setosa             5.006
#> 2 versicolor             5.936
#> 3  virginica             6.588

Alternatively, one can detach the package that has the wrong function:

detach(package:Hmisc)
ddply(iris, "Species", summarize, mean_sepal_length = mean(Sepal.Length))
#> Species mean_sepal_length
#> 1     setosa             5.006
#> 2 versicolor             5.936
#> 3  virginica             6.588

Finally, if one needs both packages and does not want to bother with ::, one can load them in the other order:

library(Hmisc)
library(plyr)

ddply(iris, "Species", summarize, mean_sepal_length = mean(Sepal.Length))
#> Species mean_sepal_length
#> 1     setosa             5.006
#> 2 versicolor             5.936
#> 3  virginica             6.588
TheLousyLinguist

I had a similar problem (with a different data set, but same error message), but I discovered that ddplyr used the UK spelling "summarise". Once I made the spelling change, code worked.

Here's the code I used. When I used the "z" spelling, I got the error message Error in .fun(piece, ...) : argument "by" is missing, with no default; but changing to "s" solved it.

library(plyr)
ddply(InsectSprays,.(spray),summarise,sum=sum(count))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!