R 中 xtable 的变量概述 [英] Variables Overview with xtable in R

查看:31
本文介绍了R 中 xtable 的变量概述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以从命令 str(x) 创建一个 xtable 以从您使用的变量中获得概览.这将是向某人介绍数据集的一个很好的功能,但自己创建它很烦人.所以我尝试的是制作一个 xtable 像这样:

I'm wondering if it's possible to create a xtable from the command str(x) to get an overview from the variables you use. This would be a nice feature to introduce someone to the dataset, but it's annoying to create it by yourself. So whta I tried is to make a xtable like this:

str(cars)
require(xtable)
xtable(str(cars))

汽车数据集来自 R.不幸的是,xtable 没有为 str() 提供 Latexcode.这里有可能智胜R吗?以下是 xtable 会理解的主要命令:

the cars dataset is given from R. Unfortunately xtable doesn't give a Latexcode for str(). Is it possible outsmart R here? Here are the main commands that xtable will understand:

methods(xtable)

有什么想法吗?

推荐答案

因为 xtabledata.framesmatrix 一起使用时可提供最佳结果> 对象,我会推荐这样的:

Since xtable provides best result when used with data.frames and matrix objects, I'd recommend something like this:

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} 

抱歉输出太长.您可以在此处获取 PDF.each 是一个非常通用的函数,因为您可以很容易地定义自定义摘要.此外,str 将输出返回到 stdout,因此您无法检索特定变量的摘要.在这种情况下,sapply 将简化结果,生成 matrix 而不是 data.frame.但这不是问题,对吧?

Sorry for lengthy output. You can grab PDF here. each is a very versatile function, since you can define custom summary quite easy. Besides, str returns output to stdout, so you can't retrieve summary for specific variables. In this case, sapply will simplify the result, yielding matrix instead data.frame. But that's not so problematic, right?

这篇关于R 中 xtable 的变量概述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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