0

used the generic Summary() function to get some data. Now i want to display some of the summary data into a table and then knit into pdf. How to I create a table on the results from calling the Summary() function?

Using TeX, kableExtra and ggplot2.

summary(segmentdata)
summary(subset(segmentdata, Segment == "Suburb mix"))
summary(subset(segmentdata, Segment == "Urban hip"))
summary(subset(segmentdata, Segment == "Travelers"))
summary(subset(segmentdata, Segment == "Moving up"))

E.g. data

age gender income kids ownHome
Min. :20.00 Length:300 Min. :-13292 Min. :0.000 Length:300
1st Qu.:32.75 Class :character 1st Qu.: 38122 1st Qu.:0.000 Class :character
Median :39.00 Mode :character Median : 51134 Median :1.000 Mode :character
Mean :40.59 Mean : 50259 Mean :1.163
3rd Qu.:47.00 3rd Qu.: 63001 3rd Qu.:2.000
Max. :70.00 Max. :139679 Max. :5.000
subscribe Segment
Length:300 Length:300
Class :character Class :character
Mode :character Mode :character

  • 1
    Welcome to Stackoverflow, please check the [How to make a great R reproducible example, here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Additionally, you didn't mention which tex editor you are using (Microsoft Word, TeX?). – Álvaro A. Gutiérrez-Vargas Jan 27 '21 at 15:56

1 Answers1

1

Welcome to StackOverflow. It's a good practice when posting a question to provide actual data with a reproducible example so contributors can help you. reprex package is recommended with R.

I'll give you an answer based on what I think you want to achieve. I used the iris data set as an example.

library(tidyverse)
library(kableExtra)

vars <- iris %>% names()
iris %>%
  filter(Species == "setosa") %>%       # subset data
  map_dfr(summary) %>%                  # apply summary to variables
  add_column(vars = vars, .before = 1)  # add variable names

#> # A tibble: 5 x 10
#>   vars  Min.  `1st Qu.` Median Mean  `3rd Qu.` Max.  setosa versicolor virginica
#>   <chr> <tab> <table>   <tabl> <tab> <table>   <tab>  <int>      <int>     <int>
#> 1 Sepa.. 4.3   4.8       5.0    5.006 5.200     5.8       NA         NA        NA
#> 2 Sepa.. 2.3   3.2       3.4    3.428 3.675     4.4       NA         NA        NA
#> 3 Peta.. 1.0   1.4       1.5    1.462 1.575     1.9       NA         NA        NA
#> 4 Peta.. 0.1   0.2       0.2    0.246 0.300     0.6       NA         NA        NA
#> 5 Spec..  NA    NA        NA       NA    NA      NA       50          0         0

For more detail on the process, check out the function's documentation.

For the kableExtra output, add kbl() %>% kable_styling() at the end of the pipeline.

Carlos Davila
  • 129
  • 2
  • 9