Edit the code chunks below and knit the document. You can pipe your objects to glimpse() or print() to display them.

UK Baby Names

Here we will convert the data table scotbabynames from the ukbabynames package to a tibble and assign it the variable name sbn. Use this data tibble for questions 1-13.

# do not alter this code chunk
sbn <- as_tibble(scotbabynames) # convert to a tibble

Question 1

How many records are in the dataset?

nrecords <- NULL

Question 2

Remove the column rank from the dataset.

norank <- NULL

Question 3

What is the range of birth years contained in the dataset? Use summarise to make a table with two columns: minyear and maxyear.

birth_range <- NULL

Question 4

Make a table of only the data from babies named Hermione.

hermiones <- NULL

Question 5

Sort the dataset by sex and then by year (descending) and then by rank (descending).

sorted_babies <- NULL

Question 6

Create a new numeric column, decade, that contains the decade of birth (1990, 2000, 2010). Hint: see ?floor

sbn_decade <- NULL

Question 7

Make a table of only the data from male babies named Courtney that were born between 1988 and 2001 (inclusive).

courtney <- NULL

Question 8

How many distinct names are represented in the dataset? Make sure distinct_names is an integer, not a data table.

distinct_names <- NULL

Question 9

Make a table of only the data from the Scottish female babies named Frankie that were born before 1990 or after 2015. Order it by year.

frankie <- NULL

Question 10

How many total babies in the dataset were named ‘Emily’? Make sure emily is an integer, not a data table.

emily <- NULL

Question 11

How many distinct names are there for each sex?

names_per_sex <- NULL

Question 12

What is the most popular name in the sbn dataset? Make sure most_popular_scottish_name is a character vector, not a table.

most_popular_scottish_name <- NULL

Question 12b

What is the most popular name for each nation and sex in the ukbabynames dataset? Make a table with the columns nation, male and female, with three rows: one for each nation.

most_popular <- NULL

Question 13

How many babies were born each year for each sex? Make a plot where the y-axis starts at 0 so you have the right perspective on changes.

babies_per_year <- NULL

Select helpers

Load the dataset reprores::personality.

Select only the personality question columns (not the user_id or date).

q_only <- NULL

Select the user_id column and all of the columns with questions about openness.

openness <- NULL

Select the user_id column and all of the columns with the first question for each personality trait.

q1 <- NULL

Window fuctions

The code below sets up a fake dataset where 10 subjects respond to 20 trials with a dv on a 5-point Likert scale.

set.seed(10)

fake_data <- tibble(
  subj_id = rep(1:10, each = 20),
  trial = rep(1:20, times = 10),
  dv = sample.int(5, 10*20, TRUE)
)

Question 14

You want to know how many times each subject responded with the same dv as their last trial. For example, if someone responded 2,3,3,3,4 for five trials they would have repeated their previous response on the third and fourth trials. Use an offset function to determine how many times each subject repeated a response.

repeated_data <- NULL

Question 15

Create a table too_many_repeats with the subject who have the two highest-ranked and second-highest ranked unique repeats values from repeated_data using ranking functions. For example, if 3 people are tied for the highest value and 2 people are tied for the next-highest value, the table would return 5 people. (Hint: check the differences among rank(), min_rank() and dense_rank())

too_many_repeats <- NULL

Advanced Questions

There are several ways to complete the following two tasks. Different people will solve them different ways, but you should be able to tell if your answers make sense.

Question 16

Load the dataset reprores::family_composition from last week’s exercise.

Calculate how many siblings of each sex each person has, narrow the dataset down to people with fewer than 6 siblings, and generate at least two different ways to graph this.

sib6 <- NULL
ggplot()

ggplot()

Question 17

Use the dataset reprores::eye_descriptions from last week’s exercise.

Create a list of the 10 most common descriptions from the eyes dataset. Remove useless descriptions and merge redundant descriptions.

eyes <- NULL