了解ddply错误消息 [英] understanding ddply error message

查看:98
本文介绍了了解ddply错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚为什么在使用ddply时收到错误消息.

I am trying to figure out why I am getting an error message when using ddply.

示例数据:

data<-data.frame(area=rep(c("VA","OC","ES"),each=4),
    sex=rep(c("Male","Female"),each=2,times=3),
    year=rep(c(2009,2010),times=6),
    bin=c(110,120,125,125,110,130,125,80,90,90,80,140),
    shell_length=c(.4,4,1,2,.2,5,.4,4,.8,4,.3,4))

bin7<-ddply(data, .(area,year,sex,bin), summarize,n_bin=length(shell_length))

错误消息: .fun(piece,...)中的错误:缺少参数"by",没有默认值

Error message: Error in .fun(piece, ...) : argument "by" is missing, with no default

我昨天收到此错误消息.我重新启动了R并重新运行了代码,一切都很好.今天早上,我再次收到错误消息,并且重新启动R不能解决问题.

I got this error message yesterday. I restarted R and reran the code and everything was fine. This morning I got the error message again and restarting R did not solve the problem.

我还尝试运行一些示例代码,并得到了相同的错误消息.

I also tried to run some example code and got the same error message.

  # Summarize a dataset by two variables
require(plyr)
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)
)

# Note the use of the '.' function to allow
# group and sex to be used without quoting
ddply(dfx, .(group, sex), summarize,
 mean = round(mean(age), 2),
 sd = round(sd(age), 2))

R信息

R version 3.2.1 (2015-06-18)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets 
[7] methods   base     

other attached packages:
 [1] Hmisc_3.17-0        ggplot2_1.0.1       Formula_1.2-1      
 [4] survival_2.38-1     car_2.0-26          MASS_7.3-40        
 [7] xlsx_0.5.7          xlsxjars_0.6.1      rJava_0.9-7        
[10] plyr_1.8.3          latticeExtra_0.6-26 RColorBrewer_1.1-2 
[13] lattice_0.20-31  

如果有人可以解释为什么会这样,我将不胜感激.

If someone could please explain why this is happening I would appreciate it.

谢谢

推荐答案

如Narendra在对该问题的评论中所述,此错误可能是由于加载了其他具有名为summarize(或summarise)的功能的软件包而引起的不能用作plyr中的功能.例如:

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

这篇关于了解ddply错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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