“标准公式接口到数据框架”是什么?在R中意味着什么? [英] What does a "standard formula interface to a data.frame" mean in R?

查看:90
本文介绍了“标准公式接口到数据框架”是什么?在R中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

aggregate 的文档指出:


'aggregate.formula'是

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

我是R的新手,我不知道这是什么手段。请解释!

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

谢谢!

Uri

推荐答案

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

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)

aggregate(),全部使用 formula 接口。上面的引用方式与整个R中使用的方法分派机制有关。

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.

考虑第一个示例:

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

因此总调度第一个参数(属于类公式)。公式在R中解析的方式通常围绕 model.matrix ,我想这里也会发生类似的情况,并且等效的调用最终会由 aggregate执行.data.frame ,使用第二个参数 chickwts ,一个 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> 

您问的不是最简单的初学者问题,我建议您仔细看一看文档和一本不错的R书(如果有的话)。 (以及其他有关SO的问题为您推荐了下一步的读物。)

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.)

编辑:我只得挖掘 aggregate.formula()不是从 stats 命名空间导出的,但是您可以通过键入 stats来查看它: :: aggregate.formula 在提示符下显示-实际上,它确实分派给 aggregate.data.frame()

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> 

这篇关于“标准公式接口到数据框架”是什么?在R中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆