1. Loading packages

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(here)
## here() starts at /cloud/project

2. Reading in birthweight data

babies <- read_csv(here("data", "birthweight_data.csv"))
## Rows: 788 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): gestation_age_w, child_ethn, plurality
## dbl (2): true_ID, birthweight
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

3. Calculating the mean birthweight separately for twins and singletons

babies %>% 
  group_by(plurality) %>% 
  summarise(birthweight = mean(birthweight)) %>% 
  ungroup()
## # A tibble: 2 × 2
##   plurality birthweight
##   <chr>           <dbl>
## 1 singleton       3248.
## 2 twin            2311.

4. Identifying the earliest (i.e. the minimum value) gestational age for each ethicity group

babies %>% 
  group_by(child_ethn) %>% 
  summarise(min_age = min(gestation_age_w)) %>% 
  ungroup()
## # A tibble: 10 × 2
##    child_ethn                        min_age
##    <chr>                             <chr>  
##  1 Aboriginal/Torres Strait Islander 33     
##  2 African/African-American          26     
##  3 Caucasian                         26     
##  4 East Asian                        33     
##  5 Hispanic/Latino                   37     
##  6 Middle-Eastern                    28     
##  7 Missing                           36     
##  8 Polynesian/Melanesian             28     
##  9 South Asian                       28     
## 10 South-East Asian                  29

5. Some notes about how group_by and summarise work with the pipe below

The pipe function allows us to clearly show in a sequential fashion the steps we take to analyse the data set. In this instance, we first pipe the “babies” dataset into the group_by funtion which seperate the data set by the ethnicity of the infants. We then use the summarise function to present in a table the minimum gestation age of the infants by ethnicity group.

6. Inserting a picture of a baby from the internet

7. Writing the summary of mean birthweight by twins/singletons to a new csv file

babies %>% 
  group_by(plurality) %>% 
  summarise(birthweight = mean(birthweight)) %>% 
  ungroup() %>% 
  write_csv("Mean_birthweight_by_plurality.csv")