运行 dmapply 中的聚合函数(ddR 包)

Running aggregate function within dmapply (ddR package)

我想要 运行 aggregate 函数 dmapply function as offered through the ddR 包中。

想要的结果

所需的结果反映了通过 aggregate 在 base:

中生成的简单输出
aggregate(
  x = mtcars$mpg,
  FUN = function(x) {
    mean(x, na.rm = TRUE)
  },
  by = list(trans = mtcars$am)
)

产生:

  trans        x
1     0 17.14737
2     1 24.39231

尝试 - ddmapply

我想在使用 ddmapply 时达到相同的结果,如下所示:

# ddR
require(ddR)

# ddR object creation
distMtcars <- as.dframe(mtcars)

# Aggregate / ddmapply
dmapply(
  FUN = function(x, y) {
    aggregate(FUN = mean(x, na.rm = TRUE),
              x = x,
              by = list(trans = y))
  },
  distMtcars$mpg,
  y = distMtcars$am,
  output.type = "dframe",
  combine = "rbind"
)

代码失败:

Error in match.fun(FUN) : 'mean(x, na.rm = TRUE)' is not a function, character or symbol Called from: match.fun(FUN)


更新

修复了 @Mike 指出的错误,删除了错误,但是没有产生预期的结果。代码:

# Avoid namespace conflict with other packages
ddR::collect(
  dmapply(
    FUN = function(x, y) {
      aggregate(
        FUN = function(x) {
          mean(x, na.rm = TRUE)
        },
        x = x,
        by = list(trans = y)
      )
    },
    distMtcars$mpg,
    y = distMtcars$am,
    output.type = "dframe",
    combine = "rbind"
  )
)

产量:

[1] trans x    
<0 rows> (or 0-length row.names)

如果您将聚合函数更改为与您之前调用的一致:FUN = function(x) mean(x, na.rm = T),它对我来说很好用。它找不到 mean(x, na.rm = T) 的原因是因为它不是一个函数(它是一个函数调用),而 mean 是一个函数。

除非您将 x = distMtcars$mpg 更改为 x = collect(distMtcars)$mpg,否则它还会给您 NA 结果。 y 也一样。综上所述,我认为这应该适合你:

res <-dmapply(
  FUN = function(x, y) {
    aggregate(FUN = function(x) mean(x, na.rm = TRUE),
              x = x,
              by = list(trans = y))
  },
  x = list(collect(distMtcars)$mpg),
  y = list(collect(distMtcars)$am),
  output.type = "dframe",
  combine = "rbind"
)

然后你可以做collect(res)看看结果。

collect(res)
#  trans        x
#1     0 17.14737
#2     1 24.39231