1

对于实验室数据,测量值通常带有检测/报告限值和置信区间。例如,我可能测量了水中的镁浓度,其中最小报告值为 5,我收到了两次测量,第一次是 10,第二次是“<5”(即低于报告值)。作为最终用户,有时您希望“<5”被视为“5”,有时被视为“0”,有时被视为“2.5”。

我解决这个问题的方法是构造一个带有属性 LRL(报告下限)的 S3 类。我希望能够让用户执行以下操作:

a <- set_measurement("<5", LRL = 5)
b <- set_measurement(8, LRL = 5)
set_conservatism(1) # sets a global variable called "conservatism_coefficient" to 1
a
# 5 [LRL: 5]
c <- b + a
# 13 [LRL: 5]
set_conservatism(0.5)
a
# 2.5 [LRL: 5]
b + a
# 10.5 [LRL: 5]
c
# 13 [LRL: 5]

我想象的是“a”的值以某种方式设置为“LRL*conservatism_co-efficient”而不是一个数字。然后当其他一些函数试图访问该值时,该值是根据当前的 conservatism_co 动态计算的-高效的。

这可能吗,和/或我只是以完全错误的方式去做这件事?

4

1 回答 1

1

不要害怕尝试重载所需的泛型函数。print您可以通过修改函数和算术运算组来实现您想要的Ops

set_conservatism = function(factor) {
    # Set global CONSERVATISM
    CONSERVATISM <<- factor
}

set_measurement = function(value, lrl=5) {
    # Create a new measurement
    v_ = "measurement"  # Dummy identifier

    # Set attributes of a measurement
    measurement = structure(v_, "value"=value, "lrl"=lrl)
    # Set class attribute of measurement
    class(measurement) = "measurement"
    measurement
}

update_measurement = function(x) {
    # Return value of measurement based on CONSERVATISM
    if (attr(x, "value") < attr(x, "lrl")) {
        attr(x, "lrl") * CONSERVATISM
    } else {
        attr(x, "value")
    }
}

print.measurement = function(x, ...) {
    # UserMethod for printing a measurement
    update_measurement(x)
}

Ops.measurement = function(e1, e2) {
    # UserMethod for arithmetic operations with measurements
    e1 = update_measurement(e1)
    e2 = update_measurement(e2)
    NextMethod(.Generic)
}

a = set_measurement(0)  # Any value smaller than lrl will do
b = set_measurement(8)

set_conservatism(1)

a + b
>>> 13

set_conservatism(0.5)

a + b
>>> 10.5

(来自 Python 程序员的旁注:使用 Python 中的属性和重写魔术方法很容易实现这样的事情)

于 2020-05-26T21:42:25.667 回答