Adwoa Nanor
Adwoa Nanor

Reputation: 11

How to create your own summary in R

I need to summarize the TitanicSurvival age dataset in R.

I need the table to look like this:

summary of TitanicSurvival

I tried to use the summary function like this:

summary(TitanicSurvival$age) 

But I got this:

   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
 0.1667 21.0000 28.0000 29.8811 39.0000 80.0000     263 

How can I perform the analysis as indicated in the image?

Upvotes: 0

Views: 156

Answers (2)

Vincent
Vincent

Reputation: 17868

You can try the datasummary function in the modelsummary package (self-promotion alert). This function uses the same formula syntax as the tables package, but it can save tables to HTML, Word, LaTeX, JPG, and more.

library(carData)
library(modelsummary)
datasummary(
  N + Mean + SD ~ age * sex,
  data = TitanicSurvival
)
female male
N 466 843
Mean 28.69 30.59
SD 14.58 14.28

Upvotes: 0

Ian Campbell
Ian Campbell

Reputation: 24888

Here's an approach with the Tidyverse packages and the gt package to create the table:

library(carData)
library(dplyr)
library(tidyr)
library(gt)

TitanicSurvival %>%
  group_by(sex) %>%
  summarise(`Sample Size` = n(),
            Mean = mean(age, na.rm = TRUE),
            `Standard Deviation` = sd(age, na.rm = TRUE)) %>%
  pivot_longer(-sex) %>%
  pivot_wider(names_from = "sex") %>%
  set_names(c("Variable","Female passengers","Male passengers")) -> TitanicSummary

TitanicSummary
## A tibble: 3 x 3
#  Variable           `Female passengers` `Male passengers`
#  <chr>                            <dbl>             <dbl>
#1 Sample Size                      466               843  
#2 Mean                              28.7              30.6
#3 Standard Deviation                14.6              14.3

gt(TitanicSummary,rowname_col = "Variable") %>%
  fmt_number(columns = everything(),
             drop_trailing_zeros = TRUE)

enter image description here

Upvotes: 1

Related Questions