作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想运行 aggregate
dmapply
内的函数通过 ddR
提供的功能包裹。
预期结果
所需的结果反射(reflect)了通过 aggregate
生成的简单输出在基地:
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)
# 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
是一个函数。
它也会给你 NA
结果除非你改变你的 x = distMtcars$mpg
至 x = collect(distMtcars)$mpg
. 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
关于在 dmapply(ddR 包)中运行聚合函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43870520/
我想运行 aggregate dmapply 内的函数通过 ddR 提供的功能包裹。 预期结果 所需的结果反射(reflect)了通过 aggregate 生成的简单输出在基地: aggregate(
我是一名优秀的程序员,十分优秀!