2

I want to use summary of each column of a data.table or data.frame to be used for sweave with xtable package. Here is MWE.

summary(iris)
  Sepal.Length    Sepal.Width     Petal.Length    Petal.Width          Species  
 Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100   setosa    :50  
 1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300   versicolor:50  
 Median :5.800   Median :3.000   Median :4.350   Median :1.300   virginica :50  
 Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199                  
 3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800                  
 Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500                  

library(xtable)

lapply(iris, summary)
$Sepal.Length
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  4.300   5.100   5.800   5.843   6.400   7.900 

$Sepal.Width
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  2.000   2.800   3.000   3.057   3.300   4.400 

$Petal.Length
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  1.000   1.600   4.350   3.758   5.100   6.900 

$Petal.Width
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.100   0.300   1.300   1.199   1.800   2.500 

$Species
    setosa versicolor  virginica 
        50         50         50 
xtableList(lapply(iris, summary))
Error in xtable.table(x[[i]], caption = caption, label = label, align = align,  : 
  xtable.table is not implemented for tables of > 2 dimensions

Wonder how to get summary of each column in separate table to be used for sweave or knitr. Thanks in advance.

MYaseen208
  • 22,666
  • 37
  • 165
  • 309
  • 4
    `xtable` doesn't have a method for normal summaries. You could turn it into a data.frame, e.g. `summary(iris) %>% as.data.frame() %>% xtable::xtable()`, though that obviously will give you a different look. `knitr::kable` seems happier with summaries: `summary(iris) %>% knitr::kable()` – alistaire Jul 03 '16 at 18:36
  • Using the `broom` package, you can `tidy` the data frame. `tidy` will return a data frame of summary statistics that will pass to `xtable` nicely. – Benjamin Jul 03 '16 at 19:49
  • 1
    `broom` helps to convert model fit output into a `data.frame`, but won't help us here. @alistaire's suggestion of `kable` should do the trick. – Keith Hughitt Jul 03 '16 at 19:53
  • @Benjamin I was going to use `broom::tidy()`, but if you look at the source of the `tidy.table` method, all it does is call `as.data.frame(x)`, which is why they return the same thing. – alistaire Jul 03 '16 at 20:06
  • @alistaire, I had in mind the `tidy.data.frame` method. Just another way to get to a similar outcome. https://github.com/dgrtwo/broom/blob/master/R/data.frame_tidiers.R – Benjamin Jul 03 '16 at 20:09
  • @Benjamin That's not the method that will get called, though. If you look at `str(summary(iris))` or `class(summary(iris))` (or `iris[,1]`), summaries return objects with a class of `table` or `summaryDefault`, which is what determines what method gets used. If you do force it into `tidy.data.frame`, it actually gives you a lot of irrelevant columns like "kurtosis" (because of the `psych::describe` call, I assume). – alistaire Jul 03 '16 at 20:20
  • I wouldn't have tidied the summary, but used `tidy (iris)`. It would require selecting the desired columns. But now that I look at the documentation again, I realize that `tidy.data.frame` doesn't return quartiles. So I will have to retract my suggestion anyway. – Benjamin Jul 03 '16 at 20:27

0 Answers0