Instructions from Jenny

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…

  • choose packages/functions
  • read in data
  • group_by and summarise
  • make notes using RMarkdown
  • insert pictures in an Rmd document
  • write data to csv

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

Self Test Steps

1. Customise your Rmd document by adding your name as the author, a table of contents and choosing a theme that you like.

2. Load the packages you will need

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)

3. Read the birthweight data

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")

4. Calculate the mean birthweight separately for twins and singletons

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.

5. identify the earliest (i.e. the minimum value) gestational age for each ethicity group

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

6. write some notes about how group_by and summarise work with the pipe below, including a link to documentation or a blog post that you think is useful

https://dplyr.tidyverse.org/reference/summarise.html

7. download a picture of a baby from the internet and insert it into your document below

8. write the summary of mean birthweight by twins/singletons that you made in step 3 above to a new csv file

write_csv(plurality_mean_birthweight_summary, "twins_and_singletons_mean_birthweight.csv")

9. Knit your document and publish the output to RPubs