Practical 4 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).

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_04 or similar!)

For each of the tasks in Analyses, 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.

This data is different from previous weeks! Make sure you use the correct URL below, otherwise you will not be able to complete the tasks properly.

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

If you are working on University computer, use the following code instead:

library(dplyr)
library(readr)

gensex <- readr::read_csv("https://and.netlify.app/datasets/gensex_2022_messy.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.

Task 4

Using R, calculate the number of cases in the dataset before any cleaning and store it in a new object called n_initial.

n_initial <- nrow(gensex)

Task 5

Using the Codebook, change the gender variable into a factor and give its levels the right labels. Then, print out a table of the gender variable.

gensex <- gensex %>% 
  dplyr::mutate(gender = factor(gender, labels = c("Male", "Female", "Other"))
  )

gensex %>% 
  dplyr::pull(gender) %>% 
  table()
.
  Male Female  Other 
    50    257      6 

Task 6

Identify any rows in the dataset from participants that did not answer any of the rating scale questions, or that were too young to ethically participate in this study. Print out a tibble of these rows, then remove them from the dataset.

gensex %>% 
  dplyr::filter(rowSums(is.na(.)) >= 10 | age < 18)
# A tibble: 10 x 13
   duration   age gender gender_comfort gender_masc gender_fem
      <dbl> <dbl> <fct>           <dbl>       <dbl>      <dbl>
 1      164    17 Female              9           4          8
 2       32    20 Female             NA          NA         NA
 3       23    18 Female             NA          NA         NA
 4       74    15 Male                9           4          6
 5      251    18 Female             NA          NA         NA
 6      234    16 Female              9           5          6
 7       53    16 Other               9           3          7
 8      523    20 Female             NA          NA         NA
 9      417    18 Female             NA          NA         NA
10      123    21 Male               NA          NA         NA
# ... with 7 more variables: gender_stable <dbl>,
#   sexual_strength <dbl>, sexual_freq <dbl>, sexual_gender <dbl>,
#   romantic_strength <dbl>, romantic_freq <dbl>,
#   romantic_gender <dbl>
gensex <- gensex %>% 
  dplyr::filter(rowSums(is.na(.)) < 10 & age >= 18)

Task 7

Using R, calculate the number of cases in the cleaned dataset and store it in a new object called n_final.

n_final <- nrow(gensex)

Knit!

Task 8

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, bring it to the next 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!