【发布时间】:2018-08-27 16:29:28
【问题描述】:
我正在尝试将 S3“数学”组泛型用于自定义类。但是我得到了一个奇怪的结果:log() 有效,而 log2 和 log10 产生错误。下面是一个最小的例子:
# simple class with just the new name
lameclass <- function(x) {
class(x) <- append(class(x), "lame")
x
}
# It prints something when Math generics methods are used
Math.lame <- function(x, ...) {
print("I am lame")
NextMethod()
}
# an object of the class
lamevector <- lameclass(1:10)
> class(lamevector)
[1] "integer" "lame"
现在尝试拨打log:
log(lamevector)
[1] "I am lame"
[1] 0.0000000 0.6931472 1.0986123 1.3862944 1.6094379 1.7917595 1.9459101 2.0794415 2.1972246 2.3025851
以 2 为基数:
log(lamevector, 2)
[1] "I am lame"
[1] 0.000000 1.000000 1.584963 2.000000 2.321928 2.584963 2.807355 3.000000 3.169925 3.321928
以上所有工作。但是现在log2 wrapper:
log2(lamevector)
[1] "I am lame"
[1] "I am lame"
Error in log2.default(1:10, 2) :
2 arguments passed to 'log2' which requires 1
也许有人可以帮助我弄清楚这里发生了什么? log2 是否真的经历了 2 次通用数学定义并失败了?
【问题讨论】:
-
log2和log10不在 S3 数学组中。