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
birthweight <- read.csv(file = "data/birthweight_data.csv")
#Mean birthweights for singeltons and twins
my_birthweightsumplu <- birthweight %>%
group_by(plurality) %>%
summarise(
birthweight = mean(birthweight)
)
ungroup(my_birthweightsumplu)
## # A tibble: 2 × 2
## plurality birthweight
## <chr> <dbl>
## 1 singleton 3248.
## 2 twin 2311.
print(my_birthweightsumplu)
## # A tibble: 2 × 2
## plurality birthweight
## <chr> <dbl>
## 1 singleton 3248.
## 2 twin 2311.
#Miniumum gestational age for each ethnicity
my_mingestationalage <- birthweight %>%
group_by(child_ethn) %>%
summarise(
mingestationalage = min(gestation_age_w)
)
ungroup(my_mingestationalage)
## # A tibble: 10 × 2
## child_ethn mingestationalage
## <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
print(my_mingestationalage)
## # A tibble: 10 × 2
## child_ethn mingestationalage
## <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
Put very simply, to summarise work you can use the group_by function which will mean you will have a result for each of the levels within the varible you are grouping by and then can use the summarise function to find any descriptive stats you want to know about that group_by
Here is a link to a blog post, it discusses the summarise and group by function using dplyr and has some useful info about all the function compatible will the summarise function.
Here is a cute picture of a baby
#Mean birthweights for singeltons and twins
my_birthweightsumplu <- birthweight %>%
group_by(plurality) %>%
summarise(
birthweight = mean(birthweight)
)
ungroup(my_birthweightsumplu)
## # A tibble: 2 × 2
## plurality birthweight
## <chr> <dbl>
## 1 singleton 3248.
## 2 twin 2311.
write_csv(my_birthweightsumplu, file = "my_birthweightsumplu.csv")