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

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

I am loading the tidyverse package. Tidyverse contains functions to read in the data read_csv and to create gruoped summaries (group_by and summarise). The here package makes it easy to tell R where the data is when you are reading it in.

# load packages 
library(tidyverse)
library(here)

3. read the birthweight data

The data is in .csv format so I am giong to use the read_csv() function. This call tells R to find the data “here” within the data folder and to make a new object called data

# read the data 
data <- read_csv(file = "/cloud/project/data/birthweight_data.csv")
data
## # A tibble: 788 × 5
##    true_ID birthweight gestation_age_w child_ethn               plurality
##      <dbl>       <dbl> <chr>           <chr>                    <chr>    
##  1    3100        3030 39              Middle-Eastern           singleton
##  2    3101        3710 40              Caucasian                singleton
##  3    3102        3770 42              African/African-American singleton
##  4    3103        3660 38              Caucasian                singleton
##  5    3104        3800 39              Caucasian                singleton
##  6    3105        3540 41              Caucasian                singleton
##  7    3106        3400 37              South-East Asian         singleton
##  8    3107        3650 39              Middle-Eastern           singleton
##  9    3108        3460 39              South-East Asian         singleton
## 10    3109        3380 39              South-East Asian         singleton
## # ℹ 778 more rows

4. calculate the mean birthweight separately for twins and singletons

By using the group_by and summarise call to calculate the mean birthweight separately for twins and singletons. It is good practice to use ungroup() in case you want to pipe more operations on to the list later.

data %>% 
  group_by(plurality) %>% 
  summarise(mean_birthweight = mean(birthweight)) %>% 
  ungroup()
## # 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

Using group_by and summarise to identify the minimum birthweight baby in each ethnicity, using the min() function. Again, remembering to ungroup()

data %>% 
  group_by(child_ethn) %>% 
  summarise(min_gestation_age = min(gestation_age_w)) %>% 
  ungroup()
## # A tibble: 10 × 2
##    child_ethn                        min_gestation_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

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

The pipe allows you to string together a number of code operations into a sequence of actions that you can do with your data. For example, it is useful to produce descriptive summaries separately for each group in your data set. By taking the dataframe, piping it to group_by, then piping it again to summarise, we can easily calculate means separtely for each group. This link has useful information.

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

Here I am adding another pipe operation

data %>% 
  group_by(plurality) %>% 
  summarise(mean_birthweight = mean(birthweight)) %>% 
  ungroup() %>%  
  write_csv("mean_birthweight_by_pluraity.csv")

9. Knit your document and publish the output to RPubs

https://rpubs.com/ezipparo1/Week1