use*_*124 12 statistics r xtable
我想知道是否可以xtable
从命令创建一个从str(x)
您使用的变量中获取概述.将某人引入数据集这是一个很好的功能,但是自己创建它会很烦人.所以我试过的是xtable
这样的:
str(cars)
require(xtable)
xtable(str(cars))
Run Code Online (Sandbox Code Playgroud)
汽车数据集来自R.不幸的xtable
是没有给出Latexcode str()
.这可能是智取R吗?以下是xtable
将要理解的主要命令:
methods(xtable)
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
Ram*_*ath 16
另一个要看的包是reporttools
.这是一段简短的代码,用于说明它在包中的tips
数据集上的用法reshape
.两个摘要语句都生成乳胶代码,可以将其复制粘贴到文档中,或用于编织.
library(reporttools)
data(tips, package = 'reshape')
# summarize numeric variables
tableContinuous(tips[,sapply(tips, is.numeric)])
# summarize non-numeric variables
tableNominal(tips[,!sapply(tips, is.numeric)])
Run Code Online (Sandbox Code Playgroud)
编辑.如果你真的必须使用str
,那么这是一种方法
str_cars = capture.output(str(cars))
xtable(data.frame(str_cars))
Run Code Online (Sandbox Code Playgroud)
来自报告的输出:
chl*_*chl 14
如果您愿意花一些时间来研究Hmisc包的工作原理,您很快就会发现有许多实用程序可以帮助完成这些任务.特别地,该contents()
方法通过报告促进了对data.frame的描述
名称,标签(如果有),单位(如果有),因子水平数(如果有),因子水平,类别,存储模式和NA数量
标签和单位可以绑定(内部,作为attributes
)每个变量.有相关的print
,html
以及latex
查看和导出的方法.
另一个不错的功能是describe()
功能,如下所示:
> describe(cars)
cars
2 Variables 50 Observations
--------------------------------------------------------------------------------
speed
n missing unique Mean .05 .10 .25 .50 .75 .90
50 0 19 15.4 7.0 8.9 12.0 15.0 19.0 23.1
.95
24.0
4 7 8 9 10 11 12 13 14 15 16 17 18 19 20 22 23 24 25
Frequency 2 2 1 1 3 2 4 4 4 3 2 3 4 3 5 1 1 4 1
% 4 4 2 2 6 4 8 8 8 6 4 6 8 6 10 2 2 8 2
--------------------------------------------------------------------------------
dist
n missing unique Mean .05 .10 .25 .50 .75 .90
50 0 35 42.98 10.00 15.80 26.00 36.00 56.00 80.40
.95
88.85
lowest : 2 4 10 14 16, highest: 84 85 92 93 120
--------------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
因为与对象xtable
一起使用时提供了最好的结果,我推荐这样的东西:data.frames
matrix
library(xtable)
library(plyr)
dtf <- sapply(mtcars, each(min, max, mean, sd, var, median, IQR))
xtable(dtf)
% latex table generated in R 2.12.2 by xtable 1.5-6 package
% Thu May 5 19:40:08 2011
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrrrrrrrrr}
\hline
& mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb \\
\hline
min & 10.40 & 4.00 & 71.10 & 52.00 & 2.76 & 1.51 & 14.50 & 0.00 & 0.00 & 3.00 & 1.00 \\
max & 33.90 & 8.00 & 472.00 & 335.00 & 4.93 & 5.42 & 22.90 & 1.00 & 1.00 & 5.00 & 8.00 \\
mean & 20.09 & 6.19 & 230.72 & 146.69 & 3.60 & 3.22 & 17.85 & 0.44 & 0.41 & 3.69 & 2.81 \\
sd & 6.03 & 1.79 & 123.94 & 68.56 & 0.53 & 0.98 & 1.79 & 0.50 & 0.50 & 0.74 & 1.62 \\
var & 36.32 & 3.19 & 15360.80 & 4700.87 & 0.29 & 0.96 & 3.19 & 0.25 & 0.25 & 0.54 & 2.61 \\
median & 19.20 & 6.00 & 196.30 & 123.00 & 3.70 & 3.33 & 17.71 & 0.00 & 0.00 & 4.00 & 2.00 \\
IQR & 7.38 & 4.00 & 205.18 & 83.50 & 0.84 & 1.03 & 2.01 & 1.00 & 1.00 & 1.00 & 2.00 \\
\hline
\end{tabular}
\end{center}
\end{table}
Run Code Online (Sandbox Code Playgroud)
对不起,输出很长.你可以在这里抓PDF .each
是一个非常通用的功能,因为您可以很容易地定义自定义摘要.此外,str
返回输出到stdout,因此您无法检索特定变量的摘要.在这种情况下,sapply
将简化结果,matrix
而不是屈服data.frame
.但这不是那么有问题,对吗?
归档时间: |
|
查看次数: |
8177 次 |
最近记录: |