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
# Install packages
library(tidyverse) # Package for pipes
library(here) # Package to make it easier to read data in
#Read the data 'here' withhin the folder 'data' and specifically read in the 'birthweight' data
babyweight <- read.csv(here("data", "birthweight_data.csv"))
# print(babies) # code that shows the table of ID< birthweight and gestational age variables
Hmm this is trickier for me.
# Calculate the mean birthweight
# babyweight%>%summarise(mean_wei = mean(birthweight)) #does not separate into twins and singletons. Use grouping
babyweight %>%
group_by(plurality) %>% # plurality variable refers to twins or singletons in data
summarise(mean_bw = mean(birthweight)) %>%
ungroup()
## # A tibble: 2 × 2
## plurality mean_bw
## <chr> <dbl>
## 1 singleton 3248.
## 2 twin 2311.
# this means we have grouped the data by plurality into a group of twins and singletons. then we summarised the data to find mean birthweight for each group :D
So we need to group by gestational age and ethnicity group and then minimum
babyweight %>%
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
#use the pipe %>% whenever you need the next line to follow on from data just created, like a pipeline
Notes are written in the comments.
Remember that images do not go in the code chunks.
#copy the same code from step 3 and add a write.csv line
babyweight %>%
group_by(plurality) %>%
summarise(mean_bw = mean(birthweight)) %>%
ungroup() %>%
write.csv("bw_by_plurality.csv" ) #only this name name required