当前位置 : 主页 > 手机开发 > 其它 >

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

来源:互联网 收集:自由互联 发布时间:2021-06-22
我想在 dmapply 函数中运行 dmapply 函数中的聚合函数. 期望的结果 期望的结果反映了通过基数聚合生成的简单输出: aggregate( x = mtcars$mpg, FUN = function(x) { mean(x, na.rm = TRUE) }, by = list(trans =
我想在 dmapply函数中运行 dmapply函数中的聚合函数.

期望的结果

期望的结果反映了通过基数聚合生成的简单输出:

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)的原因是因为它不是一个函数(它是一个函数调用),而是一个函数.

除非你将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"
)

然后你可以收集(res)来查看结果.

collect(res)
#  trans        x
#1     0 17.14737
#2     1 24.39231
网友评论