我可以在 ddply() 中进行保证金计算吗?

发布于 2024-08-17 01:26:23 字数 539 浏览 2 评论 0原文

cast() 函数非常适合计算聚合值的边距:

cast(df, IDx1+IDx2~IDy1, margins=c('IDx1','IDx2','grand_row') ,c(min,mean,max))

问题是我需要使用第二个向量和自定义函数来加权我的平均值。

当然,ddply() 允许我将自定义聚合函数应用于分组记录:

ddply(d, IDx1+IDx2~IDy1 , function(x) 
c(
min(x$value),
MyFancyWeightedHarmonicMeanFunction(x$value,x$weight),
max(x$value)
)
)

...这太棒了。

但真正能挽救局面的是同时完成这两件事的能力,无论是通过调用 cast() 中的双向量函数还是通过伪造 margins=() ddply() 中的 code> 变量。

这可能吗?

The cast() function is great at calculating margins for aggregate values:

cast(df, IDx1+IDx2~IDy1, margins=c('IDx1','IDx2','grand_row'),c(min, mean, max))

The problem is that I need to weight my means using a second vector and a custom function.

Of course, ddply() lets me apply custom aggregation functions to my grouped records:

ddply(d, IDx1+IDx2~IDy1 , function(x) 
c(
min(x$value),
MyFancyWeightedHarmonicMeanFunction(x$value,x$weight),
max(x$value)
)
)

...and this is awesome.

But what would really save the day is the ability to do both things at once, whether by calling the two-vector function in cast() or by faking somehow the margins=() variable in ddply().

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居人 2024-08-24 01:26:24

自己计算边距是很不错的:

ddply(d, "IDX1", ...) 
ddply(d, c("IDX1", "IDX2"), ...)
ddply(d, "IDy1", ...)

然后使用 rbind 将结果合并在一起。将其包装成一个通用函数并不太难。

另外,我会将您的原始代码重写为:

ddply(d, IDx1+IDx2~IDy1, summarise, 
  min = min(value),
  wt.mean = MyFancyWeightedHarmonicMeanFunction(value, weight),
  max = max(value)
)

It's pretty to compute the margins yourself:

ddply(d, "IDX1", ...) 
ddply(d, c("IDX1", "IDX2"), ...)
ddply(d, "IDy1", ...)

and then combine the results together with rbind. It wouldn't be too hard to wrap this up into a general function.

Also, I'd rewrite your original code as:

ddply(d, IDx1+IDx2~IDy1, summarise, 
  min = min(value),
  wt.mean = MyFancyWeightedHarmonicMeanFunction(value, weight),
  max = max(value)
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文