Practical 2 worksheet

Instructions

This is a marked worksheet that contains 7 questions. The questions cover topics from last week's lectures and skills lab, and the tutorial you just completed. Before you begin the worksheet, you should first read these instructions and complete the analyses described in "Analysis", below.

You will have 7 randomly selected questions to answer; do not be surprised if you have different questions from others working on the same worksheet!

To access the worksheet, you must attend your practical session. In the session, a passcode will be announced to unlock the worksheet; you must begin the worksheet within 5 minutes of the passcode being released. You will have 30 minutes to complete the worksheet, unless you have reasonable adjustments for extra time (38 minutes for 25% extra time, and 45 minutes for 50% extra time).


Academic Honesty

You are welcome to use module resources - e.g. lecture slides, tutorials, skills labs scripts - to answer these questions. You are also welcome to use RStudio to solve problems, do calculations, or refer to output. However, you should not work with other students while you are completing the worksheet, and tutors will only be able to answer questions about technical problems (e.g. computer crash).

Slides

You can find the slides from the Week 1 practical session here, but you do not need them to complete this worksheet.

Setting Up

Task 1

Open your project for this week in RStudio. Then, open a new Markdown file with HTML output and save it in the r_docs folder. (Give it a sensible name, like worksheet_01 or similar!)

For each of the tasks in Analysis, write your code to complete the task in a new code chunk.

Remember, you can add new code chunks by:

  1. Using the RStudio toolbar: Click Code > Insert Chunk
  2. Using a keyboard shortcut: the default is Ctrl + Alt + I (Windows) or ⌘ Command + Option + I (MacOS), but you can change this under Tools > Modify Keyboard Shortcuts…
  3. Typing it out: ```{r}, press ↵ Enter, then ``` again.
  4. Copy and pasting a code chunk you already have (but be careful of duplicated chunk names!)

To prepare for the take-away paper, make sure you knit this document when you’ve finished the tasks.

Task 2

Load the tidyverse package and read in the data in the setup code chunk.

library(tidyverse)
gensex <- readr::read_csv("https://and.netlify.app/datasets/gensex_2022.csv")

Task 3

Review the Codebook at the link below, which has all the information you need about this dataset.

View the Codebook

Analyses

You will need the output from all of the following tasks in order to complete the worksheet quiz. If you are having any trouble with this, or you aren’t sure how to understand what the output means, ask for help in your practical!

Task 4

Bring up the standard help documentation for the function group_by() from the dplyr package.

Use ?function_name in the Console to bring up the help documentation in the Viewer.

If you are having difficulty working in RStudio, you can also search for the help documentation online. Keywords might include R, the function name, and the package that that function exists in.

Task 5

Starting with the gensex data, do the following in a single pipeline.

Have a look at the example under “Basic Pipeline” in Tutorial 1!

gensex %>%
  dplyr::group_by(gender) %>%
  dplyr::summarise(
    mean_masc = mean(gender_masc),
    sd_masc = sd(gender_masc),
    mean_comf = mean(gender_comfort),
    sd_comf = sd(gender_comfort)
  )
# A tibble: 4 x 5
  gender mean_masc sd_masc mean_comf sd_comf
  <chr>      <dbl>   <dbl>     <dbl>   <dbl>
1 Female      3.17   1.48       8.77   0.771
2 Male        6.25   1.31       8.65   0.812
3 Other       6.6    0.548      5.6    2.61 
4 <NA>        3.67   1.15       8.67   0.577

Task 6

Starting with the gensex data, do the following in a single pipeline.

Have a look at Task 10 in Tutorial 1!

gensex %>%
  dplyr::mutate(gender_fem_z = scale(gender_fem)) %>%
  dplyr::group_by(gender) %>%
  dplyr::summarise(mean_gender_fem_z = mean(gender_fem_z))
# A tibble: 4 x 2
  gender mean_gender_fem_z
  <chr>              <dbl>
1 Female            0.279 
2 Male             -1.39  
3 Other            -0.571 
4 <NA>             -0.0409

Knit!

Task 7

Knit your worksheet once you’ve finished. You should see all of your code and output in the HTML document that it produces. This HTML will be saved in the same folder as the RMarkdown you knitted it from.

If you encounter a knitting error, ask for help in your practical!

Well done!

Make sure you have the RMarkdown or knitted HTML on hand when you take the worksheet quiz - you will need your answers to the above tasks.

Good luck!