Changing the format of a table

Hello everyone,

I am trying to create a summary table (using the package kableextra) that would indicate the mean, median, and standard deviation of two different variables called "wage" and "log_wage" from a data frame. The best I have been able to get was a table with 6 columns and 1 row using the code below.

data("CPS1988")
df <- CPS1988 %>%
  mutate(log_wage = log(wage))
Summary <- df %>%
  summarize(Mean_wage = mean(wage),
          Median_wage = median(wage),
          Standard_deviation_wage = sd(wage),
          Mean_log_wage = mean(log_wage),
          Median_log_wage = median(log_wage),
          Standard_deviation_log_wage = sd(log_wage))
Summary %>%
  kbl() %>%
  kable_classic_2()

However, this is not really the display I'm looking for. I would like 3 columns (1 for each of mean, median and standard deviation) and 2 rows (1 for wage and 1 for log_wage). Can anyone help me with this?

Thank you!

P.S. the data frame comes from the package AER.

Try reshaping before using kable.

#Invent data
library(dplyr)
library(tidyr)

DF <- data.frame(wage=runif(100,50,150))
DF <- DF |> mutate(log_wage=log10(wage))    

Summary <- DF %>%
  summarize(Mean_wage = mean(wage),
            Median_wage = median(wage),
            Standard.deviation_wage = sd(wage),
            Mean_log_wage = mean(log_wage),
            Median_log_wage = median(log_wage),
            Standard.deviation_log_wage = sd(log_wage))

Long <- pivot_longer(Summary,cols = everything())
Long <- separate(Long,col = "name",into = c("Stat","Type"),sep = "_",extra = "merge")
Long
#> # A tibble: 6 x 3
#>   Stat               Type      value
#>   <chr>              <chr>     <dbl>
#> 1 Mean               wage     98.8  
#> 2 Median             wage     96.5  
#> 3 Standard.deviation wage     30.1  
#> 4 Mean               log_wage  1.97 
#> 5 Median             log_wage  1.98 
#> 6 Standard.deviation log_wage  0.140

TwoRow <- pivot_wider(Long,names_from = "Stat",values_from = "value")
TwoRow
#> # A tibble: 2 x 4
#>   Type      Mean Median Standard.deviation
#>   <chr>    <dbl>  <dbl>              <dbl>
#> 1 wage     98.8   96.5              30.1  
#> 2 log_wage  1.97   1.98              0.140

Created on 2022-05-15 by the reprex package (v2.0.1)

Thanks that's perfect!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.