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
# load packages
library(tidyverse)
#read data
frames <- read_csv("data/birthweight_data.csv")
# Calculate mean birthweight of twins and singletons
mean_birthweight_of_twins_and_singletons <- frames %>%
group_by(plurality) %>%
summarise(mean_birthweight = mean(birthweight)) %>%
ungroup()
print(mean_birthweight_of_twins_and_singletons)
## # A tibble: 2 × 2
## plurality mean_birthweight
## <chr> <dbl>
## 1 singleton 3248.
## 2 twin 2311.
I still had trouble understanding if I had completed the code correctly, so I used the print function to have a visual representation
According to the output, the mean birthweight of twins is 2311 grams, whereas singletons have a mean of 3248 grams.
#Identifying earliest gestational age for each ethnicity group
earliest_gestational_age_per_ethnicity <- frames %>%
group_by(child_ethn) %>%
summarise( Minimum_Gestataional_Age = min(gestation_age_w)) %>%
ungroup()
print(earliest_gestational_age_per_ethnicity)
## # A tibble: 10 × 2
## child_ethn Minimum_Gestataional_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
1.From my understanding, the function of the pipe operator is to act as a ‘connector’ that passes on the results/output of the previous operation into subsequent operations 2.As such, the group_by operation gathers the relevant variables and ‘feeds’ them into the pipe thereby allowing for further analysis of the now isolated dataset. 3.Thus, the **summarise* operation is used to identify the statistical qualities of the dataset provided by the pipe, including finding the mean, median, spread, etc…
A helpful link that helped me to understand the relationship between the pipe, group_by and summarise operators can be found here
write_csv(mean_birthweight_of_twins_and_singletons, file = "mean_birthweight_of_twins_and_singletons.csv")