2

我正在尝试计算一些简单的比率并使用 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"]))
}
4

2 回答 2

1

这不是我想要的,但在其他情况下可能会有所帮助。

问了这个问题后,我发现了一些东西

为了能够引用变量,而不是数据框、对象等的一部分,可以使用点括号表示法:.(voice)是解决方案。因此,正确的函数定义如下所示:

my_ratio <- function(voice) {
  ddply(singer, ~ voice.part,
        transform,
        ratio = height / mean(height[voice.part == .(voice)]))
}

但是,它不会导致与使用文字字符串相同的行为。

于 2012-09-01T08:37:27.887 回答
1

这个怎么样:

my_ratio <- function(voice) {
  my_transform <- function(x) {
    transform(x, ratio = height / mean(height[voice.part == voice]))
  }
  ddply(singer, ~voice.part, my_transform)
}

您真的只想缩放匹配的行voice并在NaN其他地方拥有(这也是您的硬编码函数所做的)?

上述更紧凑的版本:

my_ratio <- function(voice) {
  ddply(singer, ~voice.part, 
    function(x) transform(x, ratio = height/mean(height[voice.part == voice])))
}

如果您真的想缩放所有记录(如您的评论所示):

my_ratio <- function(voice) {
  scale <- with(singer, mean(height[voice.part == voice]))
  ddply(singer, ~voice.part, 
    function(x) transform(x, ratio = height / scale))
}
于 2012-09-01T09:24:23.223 回答