gpt4 book ai didi

r - 在 R 中编写复杂表格的好方法?

转载 作者:行者123 更新时间:2023-12-01 13:07:58 25 4
gpt4 key购买 nike

有没有人对如何在 R 中编写复杂的表格有什么好的想法?

恐怕我对此可能有点含糊,但我想设置一个脚本来创建一堆类似于美国统计摘要的复杂表。

例如:http://www.census.gov/compendia/statab/tables/09s0015.pdf

我想避免一大堆 rbind 和 hbind 语句。

SAS 中,我听说有一种表创建规范语言;我想知道 R 是否有类似的功能?

谢谢!

最佳答案

您似乎想对某些数据应用多种不同的计算,将其按一个字段(在本例中,按州)分组?

有很多方法可以做到这一点。参见 this related question .

您可以使用 Hadley Wickham 的 reshape 包(参见 reshape homepage )。例如,如果您想要将均值、求和和计数函数应用于按值分组的某些数据(这是没有意义的,但它使用来自 reshape 的空气质量数据):

> library(reshape)
> names(airquality) <- tolower(names(airquality))
> # melt the data to just include month and temp
> aqm <- melt(airquality, id="month", measure="temp", na.rm=TRUE)
> # cast by month with the various relevant functions
> cast(aqm, month ~ ., function(x) c(mean(x),sum(x),length(x)))
month X1 X2 X3
1 5 66 2032 31
2 6 79 2373 30
3 7 84 2601 31
4 8 84 2603 31
5 9 77 2307 30

或者您可以使用 by() 函数。索引将代表州的位置。在您的情况下,您可以应用自己的函数来执行多项任务(取决于您的需要),而不是应用一个函数(例如 mean):例如, function(x) { c(mean(x),长度(x))。然后在输出上运行 do.call("rbind"(例如)。

此外,您可能会考虑使用报告包,例如 Sweave(带 xtable)或 Jeffrey Horner's brew package .有一个 great post on the learnr blog about creating repetitive reports这显示了如何使用它。

关于r - 在 R 中编写复杂表格的好方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1467807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com