【发布时间】:2017-09-28 18:59:17
【问题描述】:
我正在将标准 S3 方法调度系统添加到我的包 (OneR) 中,其中我有一种用于数据帧的方法和一种用于公式的方法。
我遇到的问题是两种方法都有不同的论据。调用数据框方法时我不需要data 参数,因为数据已经存在于x 参数中。 data只有在调用公式方法时才需要。
我是这样做的:
Usage
optbin(x, data, method = c("logreg", "infogain", "naive"), na.omit = TRUE)
## S3 method for class 'formula'
optbin(x, data, method = c("logreg", "infogain", "naive"),
na.omit = TRUE)
## S3 method for class 'data.frame'
optbin(x, data = x, method = c("logreg", "infogain",
"naive"), na.omit = TRUE)
Arguments
x either a formula or a data frame with the last column containing the target variable.
data data frame which contains the data, only needed when using the formula interface because otherwise 'x' will already contain the data.
method character string specifying the method for optimal binning, see 'Details'; can be abbreviated.
na.omit logical value whether instances with missing values should be removed.
我最初认为我可以在数据框方法中省略 data 参数,但是在检查包时我收到警告,因为它存在于 UseMethod 函数中......当我把它留在那里时由于方法之间的不一致,我收到了另一个警告。我也试过...,但我也收到了警告,除了我必须记录它之外,这会让用户感到困惑,而不是帮助。
但由于数据框方法中的data = x 参数,我也没有找到理想的解决方案。它可能会使人们感到困惑,并且是潜在的错误来源。
我的问题
解决这种情况的最佳方法是什么,即当您有两种具有不同参数的方法时?
【问题讨论】:
-
您不能在
optbin.data.frame中使用data = NULL并将其记录为未使用吗?对data.table::data.table中的drop参数做了类似的事情:“data.table 从未使用过。不要使用。它需要在这里,因为 data.table 继承自 data.frame。” . -
@nrussell:我认为这是一种有效的方法...请您从中创建一个答案 - 谢谢
标签: r methods documentation arguments