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
The package “tidyverse” was chosen as it is a comprehensive dataset package aids in transforming and presenting data neatly. The package “tidyverse” was loaded using the “library()” function, which is the directory in R where packages are stored.
library(tidyverse)
I created an “object”(?) called “birthweight_data” which contains the function “read_csv” and specified which csv file it should be. The “<-” assignment operator was used to store all the data in the “birthweight_data.csv” file.
birthweight_data <- read_csv(file = "data/birthweight_data.csv")
I created an object called “plurality_mean_birthweight_summary” and used the “<-” assignment operator to store all of the following data so I could print it later on. To calculate the mean birthweight separately for twins and singletons, I first used the object “birthweight_data” to read the aforementioned csv which contains the data, followed by a pipe function so the code follows the next steps sequentially. I then used the “group_by” function, which groups “birthweight_data” by any list of variables: I chose “plurality” as it distinguishes the data based on whether the child is a singleton or a twin. After using another pipe, I used the “summarise” function to summarise the data and created a object called “mean_birthweight”. The “mean_birthweight” object used the “mean” function to calculate the average of the “birthweight” variable. I then used another pipe, followed by an “ungroup” function, which is good practice if I want to add more functions/steps to the data later so it won’t remain grouped by the “plurality” variable. I then printed the object “plurality_mean_birthweight_summary” so I could see the average birthweight for twins and singletons.
plurality_mean_birthweight_summary <-birthweight_data %>%
group_by(plurality) %>%
summarise(
mean_birthweight = mean(birthweight)
) %>%
ungroup()
print(plurality_mean_birthweight_summary)
## # A tibble: 2 × 2
## plurality mean_birthweight
## <chr> <dbl>
## 1 singleton 3248.
## 2 twin 2311.
Same as above except I used the “min” function to obtain the minimum value for the “gestational_age_w” variable, which is grouped by the child’s ethnicity (“child_ethn” variable).
min_age_summary <- birthweight_data %>% group_by(
child_ethn) %>%
summarise(
min_gestational = min(gestation_age_w)
) %>%
ungroup()
print(min_age_summary)
## # A tibble: 10 × 2
## child_ethn min_gestational
## <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
write_csv(plurality_mean_birthweight_summary, "twins_and_singletons_mean_birthweight.csv")