library(learnr)
library(bst290)
knitr::opts_chunk$set(echo = FALSE)

Getting to know your data

De-bugging exercises {data-progressive=TRUE}

As before: De-bugging is unfortunately a part of working with computer code. In the following exercises, you get a piece of code that does not work and have to figure out how to fix it.

In all exercises, you will work with the small ess dataset that you used in the main tutorial and which you should therefore be familiar with (otherwise, quickly go over the main tutorial again to refresh your memory).

Problem No. 1

Here you want to calculate the average age of your respondents --- but R refuses...

average(ess$agea)
**Hint:** Was the relevant function really called `average()`? There was also this other word for 'average'...

Problem No. 1: Solution

Obviously, the problem was that the function to calculate an average in R is called mean(), and not average(). You may find that strange or stupid, but it just is that way...

Here is how you make that code work:

mean(ess$agea)

Problem No. 2

You want to see the first 20 observations of the ess dataset -- but R only shows you the first six. How can you get 20?

head(ess)
**Hint:** There is an option for the `head()` function that lets you decide how many observations are printed. `n=`...

Problem No. 2: Solution

This one is solved by setting the number of rows to 20 with the n option:

head(ess,
     n = 20)

Problem No. 3

You want to calculate the standard deviation of the agea variable, and you definitely have the correct function and variable --- but something still causes an error:

sd(ess@agea)
**Hint:** Which symbol do you need to use to select a specific variable from a dataset?

Problem No. 3: Solution

The issue was that the wrong symbol was used to indicate that R should use a specific variable from the ess dataset: This is done with the $ symbol, not with @:

sd(ess$agea)

A last quiz

quiz(caption = "Final (brief) quiz",
     question("What is the correct function to get a list of the distinct categories of a variable?",
              answer("`unique()`", correct = T),
              answer("`distinct()`", message = "Not quite - try again!"),
              answer("`categories()`", message = "Unfortunately not - but don't give up, you know this!"),
              random_answer_order = T,
              allow_retry = T),
     question("How does `R` like to store categorical variables?",
              answer("As *factors*", correct = T),
              answer("As *categoricals*", message = "Not quite - try again!"),
              answer("As *levels*", message = "Unfortunately not - but don't give up, you know this!"),
              random_answer_order = T,
              allow_retry = T),
     question("How can you get the last few observations of a dataset?",
              answer("`tail()`", correct = T),
              answer("`end()`", message = "Not quite - try again!"),
              answer("`last()`", message = "Almost...keep trying."),
              random_answer_order = T,
              allow_retry = T))

Conclusion

Congrats, you are done! Now you know how you can get summary statistics and familiarize yourself with a new dataset.



cknotz/bst290 documentation built on Nov. 11, 2023, 1 p.m.