我正在尝试计算一些简单的比率并使用 R 的括号符号来表示比率的基线。
现在我正在努力定义一个让我参数化基线的函数。我不想硬编码它,因为我有几个。而且,我真的不明白 R 到底在做什么,我很好奇如何实现所需的行为。
这里有一些基于示例数据的代码:
data("singer", package = "lattice")
# this is what I want, but what currently doesn't work
my_ratio <- function(voice) {
ddply(singer, ~ voice.part,
transform,
# how do I refer to the voice variable here?
# it looks like it misunderstands it as column?
ratio = height / mean(height[voice.part == voice]))
}
# this version works with a hardcoded voice part
my_ratio_hard <- function() {
ddply(singer, ~ voice.part,
transform,
ratio = height / mean(height[voice.part == "Soprano 1"]))
}