Search code examples
rr-markdown

Creating tables with descriptive statistics in R


I would like some help on creating formatted tables in R - whether it's just using the normal IDE or R Markdown. There are two main things that I'd like to do:

  • Present the descriptive statistics (Mean, Median, Min, Max) by group based on different columns
  • Present the descriptive statistic based on the total sample (ungrouped data)

Sample data:

   df <- data.frame(Gender = c("F", "M", "F", "M", "M", "M", "M", "F", "M", "M"),
                 Young = c("Y", "N", "Y", "N", "Y", "N", "Y", "N", "Y", "N"),
                 Age = c("14", "25", "13", "24", "14", "25", "13", "24", "10", "26"),
                 Location = c("Suburb", "Rural", "Suburb", "Rural","Suburb", "Rural","Suburb", "Rural","Suburb", "Rural"))

Expected results

Variable Mean Median Max Min
Gender
Female
Male
Location
Suburb
Rural
TOTAL

Is there a way to do this in R?


Solution

  • You can get all the information that you need by getting the data in long format.

    library(dplyr)
    library(tidyr)
    
    df <- type.convert(df, as.is = TRUE)
    
    df %>%
      pivot_longer(cols = -Age) %>%
      group_by(name, value) %>%
      summarise(min_age = min(Age), 
                max_age = max(Age), 
                median_age = median(Age), 
                mean_age = mean(Age))
    
    #  name     value  min_age max_age median_age mean_age
    #  <chr>    <chr>    <int>   <int>      <int>    <dbl>
    #1 Gender   F           13      24         14     17  
    #2 Gender   M           10      26         24     19.6
    #3 Location Rural       24      26         25     24.8
    #4 Location Suburb      10      14         13     12.8
    #5 Young    N           24      26         25     24.8
    #6 Young    Y           10      14         13     12.8