“数据框的标准公式接口”是什么? R 中的意思?

发布于 2024-12-05 04:38:27 字数 178 浏览 3 评论 0原文

aggregate 的文档指出:

“aggregate.formula”是“aggregate.data.frame”的标准公式接口。

我是R新手,不明白这是什么意思。请解释一下!

谢谢!

乌里

The documentation for aggregate states:

‘aggregate.formula’ is a standard formula interface to ‘aggregate.data.frame’.

I am new to R, and I don't understand what this means. Please explain!

Thanks!

Uri

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

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

发布评论

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

评论(1

无边思念无边月 2024-12-12 04:38:27

跳转到 help(aggregate) 示例部分的中间,您将看到:

 ## Formulas, one ~ one, one ~ many, many ~ one, and many ~ many:
 aggregate(weight ~ feed, data = chickwts, mean)
 aggregate(breaks ~ wool + tension, data = warpbreaks, mean)
 aggregate(cbind(Ozone, Temp) ~ Month, data = airquality, mean)
 aggregate(cbind(ncases, ncontrols) ~ alcgp + tobgp, data = esoph, sum)

aggregate() 的四种不同调用,全部使用公式 界面。上面你引用的内容与整个 R 中使用的方法分派机制有关。

考虑第一个例子:

R> class(weight ~ feed)
[1] "formula"
R> class(chickwts)
[1] "data.frame"

所以聚合分派它的第一个参数(类 formula)。 R 中公式的解析方式通常围绕 model.matrix,我认为这里也发生了类似的情况,并且最终由aggregate.data.frame 执行等效的调用,使用第二个参数 chickwts,一个 data.frame

R> aggregate(weight ~ feed, data = chickwts, mean)
       feed  weight
1    casein 323.583
2 horsebean 160.200
3   linseed 218.750
4  meatmeal 276.909
5   soybean 246.429
6 sunflower 328.917
R> 

你问的不是最简单的初学者问题,我建议你仔细阅读一些文档,如果你手头有一本像样的 R 书,我建议你仔细阅读。 (其他SO问题给出了接下来要读什么的建议。)

编辑:我不得不挖掘一点,因为aggregate.formula()没有从导出stats 命名空间,但您可以通过在提示符处输入 stats:::aggregate.formula 来查看它 - 然后清楚地表明它实际上会分派到 aggregate.data.frame():

 [.... some code omitted ...]
    if (is.matrix(mf[[1L]])) {
        lhs <- as.data.frame(mf[[1L]])
        names(lhs) <- as.character(m[[2L]][[2L]])[-1L]
        aggregate.data.frame(lhs, mf[-1L], FUN = FUN, ...)
    }
    else aggregate.data.frame(mf[1L], mf[-1L], FUN = FUN, ...)
}
<environment: namespace:stats>
R> 

Jump to the middle of the examples section of help(aggregate) and you will see this:

 ## Formulas, one ~ one, one ~ many, many ~ one, and many ~ many:
 aggregate(weight ~ feed, data = chickwts, mean)
 aggregate(breaks ~ wool + tension, data = warpbreaks, mean)
 aggregate(cbind(Ozone, Temp) ~ Month, data = airquality, mean)
 aggregate(cbind(ncases, ncontrols) ~ alcgp + tobgp, data = esoph, sum)

Four different calls to aggregate(), all using the formula interface. The way it is written above in what you quote has to do with method dispatching mechanism used throughout R.

Consider the first example:

R> class(weight ~ feed)
[1] "formula"
R> class(chickwts)
[1] "data.frame"

so aggregate dispatches on it first argument (of class formula). The way a formula gets resolved in R typically revolves around a model.matrix, I presume something similar happens here and an equivalent call is eventually execucted by aggregate.data.frame, using the second argument chickwts, a data.frame.

R> aggregate(weight ~ feed, data = chickwts, mean)
       feed  weight
1    casein 323.583
2 horsebean 160.200
3   linseed 218.750
4  meatmeal 276.909
5   soybean 246.429
6 sunflower 328.917
R> 

What you asked isn't the easiest beginner question, I'd recommend a good thorough look at some of the documentation and a decent R book if you have one handy. (And other SO questions give recommendation as to what to read next.)

Edit: I had to dig a little as aggregate.formula() is not exported from stats namespace, but you can look at it by typing stats:::aggregate.formula at the prompt -- which then clearly shows that it does, in fact, dispatch to aggregate.data.frame():

 [.... some code omitted ...]
    if (is.matrix(mf[[1L]])) {
        lhs <- as.data.frame(mf[[1L]])
        names(lhs) <- as.character(m[[2L]][[2L]])[-1L]
        aggregate.data.frame(lhs, mf[-1L], FUN = FUN, ...)
    }
    else aggregate.data.frame(mf[1L], mf[-1L], FUN = FUN, ...)
}
<environment: namespace:stats>
R> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文