加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:02:20 所属栏目:百科 来源:网络整理
导读:我想在 dmapply 函数中运行 dmapply 函数中的聚合函数. 期望的结果 期望的结果反映了通过基数聚合生成的简单输出: aggregate( x = mtcars$mpg,FUN = function(x) { mean(x,na.rm = TRUE) },by = list(trans = mtcars$am)) 产生: trans x1 0 17.147372 1 24
我想在 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)
        },by = list(trans = y)
      )
    },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,x = list(collect(distMtcars)$mpg),y = list(collect(distMtcars)$am),combine = "rbind"
)

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读