Welcome to the PSYC3361 coding W1 self test. The test assesses your ability to use the coding skills covered in the Week 1 online coding modules.
In particular, it assesses your ability to…
It is IMPORTANT to document the code that you write so that someone who is looking at your code can understand what it is doing. Above each chunk, write a few sentences outlining which packages/functions you have chosen to use and what the function is doing to your data. Where relevant, also write a sentence that interprets the output of your code.
Your notes should also document the troubleshooting process you went through to arrive at the code that worked.
For each of the challenges below, the documentation is JUST AS IMPORTANT as the code.
Good luck!!
Jenny
Will load the tidyverse package and the here package.The tidyverse package has function that can read the read_csv data and create grouped summaries. The here package allows us to tell R where the data is when reading it in.
library(tidyverse)
library(here)
To read the birthweight data in, use read_csv as the file is in .csv format. The here function tells R to find the data in the data folder. R will also make a new object labelled babies.
babies <- read_csv(here("data", "Sample_BirthWeight_GestAge.csv"))
To calculate the mean birthweight of twins and singletons separately, I will use group_by and summarise. Ungroup is used in case I choose to pipe more operations onto the list.
babies %>%
group_by(plurality) %>%
summarise(mean_bw = mean(birthweight)) %>%
ungroup()
## # A tibble: 2 × 2
## plurality mean_bw
## <chr> <dbl>
## 1 singleton 3248.
## 2 twin 2311.
To identify the earliest gestational age for each ethnicity group, I will use group_by and summarise to identify the minimum birthweight baby and use the min() function. I will continue to ungroup.
babies %>%
group_by(child_ethn) %>%
summarise(min_ga = min(gestation_age_w)) %>%
ungroup()
## # A tibble: 10 × 2
## child_ethn min_ga
## <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
The pipe allows for several code operations to be brought together into a sequence of actions. Piping can be useful to produce descriptive summaries for each separate group in the data set. If you use the dataframe, pipe it to group_by, and then pipe it to summarise, the means can be calculated separately for each group. A blog that was helpful was on github pages.
Summary of the mean birthweight by plurarity is made into a new csv file labelled “bw_by_plurality.csv”
babies %>%
group_by(plurality) %>%
summarise(mean_bw = mean(birthweight)) %>%
ungroup() %>%
write_csv("bw_by_plurality.csv")