Worksheet 3

Published

September 18, 2026

Questions are below. My solutions will be available after the tutorials are all finished. The whole point of these worksheets is for you to use your lecture notes to figure out what to do. In tutorial, the TAs are available to guide you if you get stuck. Once you have figured out how to do this worksheet, you will be prepared to tackle the assignment that depends on it.

If you are not able to finish in an hour, I encourage you to continue later with what you were unable to finish in tutorial.

Questions are respectively on choosing rows and columns, recoding values, and joins. There is a lot here, so you should plan to work at a good pace. You don’t have to do these questions in order. If you want practice on a certain thing, start with that one.

Packages you’ll need:

library(tidyverse)

Student stats

A survey of students at the University of California Davis asked respondents:

  • the gender they identify as (called Sex in the data set)
  • hours of TV they watched in the last week
  • hours they spent doing non-academic things on a computer in the last week (playing games, on social media etc. This survey was taken some years ago, and so pre-dates smartphones.)
  • how many hours of sleep they got last night
  • where they prefer to sit in their classes (front, middle, back of classroom)
  • how many alcoholic drinks they consumed in the last week (defined as US standard drinks)
  • their height (inches)
  • their mother’s height (inches)
  • their father’s height (inches)
  • the number of hours of exercise they got in the last week
  • their GPA
  • their area of study, classified as Liberal Arts or NonLib (“not arts” meaning science, math, engineering etc).

Students were allowed to not respond to any of the items, so there are some missing values, labelled NA in the dataframe you are about to read in.

The data are in http://datafiles.ritsokiguess.site/UCDavis1.csv.

  1. Read in and display (some of) the data.
  1. Find the mean and standard deviation of sleep times (for all the students taken together).
  1. For the students that sit in each part of the classroom (separately), find the number of students and their mean sleep time. (Hint: the column is called Seat with a capital S; the S being uppercase matters.)
  1. For each of the students this data set, display how much time they have spent watching TV and on the computer.
  1. Several of the columns are heights. Display all of these columns, without naming any columns.
  1. Display only the students who sit at the back of the classroom.
  1. Display all the students that either slept 5 hours or less or watched over 30 hours of TV (or both).
  1. For the students who are 70 or more inches tall, what are the mean heights of their mother and their father? Hint: the first time you do this, your answers will probably be missing. Why is that? Hint: to get the answers you were expecting, search for the na.rm option. What does that do?

Diet Wars 2

In an earlier worksheet, we investigated dropout rates for subjects on three different diets. The data were in http://datafiles.ritsokiguess.site/diet-wars-1.txt. To repeat the data description from earlier:

312 subjects took part in a comparative study of three diets: a low-fat diet (labelled 1 in the dataset), a Mediterranean diet (labelled 2), and a low-carbohydrate diet (labelled 3). Each subject was randomly allocated to one of the diets. One measure of success of a diet program is (of course) how much weight a subject lost, but another is whether they stayed with their diet until the end of the study (1 in the Status column) or whether they dropped out of the study (0 in the Status column).

The various different diets and status values are represented by numbers. In the questions that follow, we will be reorganizing the data so that the categories are represented by something more meaningful.

  1. Read in and display (some of) the data. For this, re-use your code from the earlier worksheet.
  1. Change the values in both the Diet and Status columns to be text that describes what they are. Specifically, use LowFat, Medit and LowCarb for the diets, and Completed and DroppedOut for the Status, making sure to match the right description to the right number. Save your updated dataframe.
  1. The principal investigator of the study meets with you and says “this is great, but could you change it so that the name of the Mediterranean diet is Mediterranean and not Medit?” Do that, and save your new dataframe.
  1. In a previous worksheet, you drew a grouped bar chart of these data. Redraw that graph with your new data, and comment on whether it is easier to interpret.

Counting seabirds

Each year, bird experts associated with the Kodiak National Wildlife Refuge in Alaska count the number of seabirds of different types on the water in each of four different bays in the area. This is done by drawing (on a map) a number of straight-line “transects”, then driving a boat along each transect and counting the number and type of birds visible within a certain distance of the boat.

Variables of interest are:

  • the Year of observation
  • the Transect number
  • Temp: the temperature
  • ObservCond: visibility, from Average up to Ideal
  • Bay the name of the bay
  • bird: an abbreviation for the species of bird observed
  • count: how many of that type of bird were observed (in that year, bay, transect).

The data are in http://datafiles.ritsokiguess.site/seabird_long.csv.

  1. Read in and display some of the data.
  1. A data set containing the full bird names and their principal diet is in http://datafiles.ritsokiguess.site/bird_names.csv. Read in and display some of this data set.
  1. It is awkward to read the bird abbreviations in the first dataframe. Create and save a new dataframe that has the full bird names as well as the number that were observed and all the other information.
  1. Which three species of bird were seen the most often altogether?
  1. Make a graph that shows the trend in total counts of each bird species over time. Think about what would make the most appealing graph to understand the time trends.