Below is an example of how to answer a question. You haven’t yet learned some of the functions we use here, but follow along for now - it’s just an example!
There are almost always multiple coding approaches to get the right answer, some better than others. As long as your code and answer are accurate and make sense, you’ll get the marks!
Question 1. What are the minimum and maximum heights (variable name is “height_cm”) of students in the “students” dataset, which is available at this URL:
As we learned in the [fictitious] “importing and exploring data” tutorial, I can use the
read_csv function from the
readr package (loaded with
tidyverse) to download and import the dataset. It creates a “tibble” object, which here I name “students”:
I also learned that it’s a good idea to get an overview of the dataset as a first step after importing data. To do this, use the
skim_without_charts function from the
skimr package. I need to load that package first:
|Number of rows||154|
|Number of columns||6|
|Column type frequency:|
Variable type: character
Variable type: numeric
This shows we have four character and three numeric variables, with 154 rows (observations) and 7 columns (variables) total.
We can use the
summary function to get some basic descriptive statistics, including the minimum and maximum of numeric variables. The
summary function is part of the base R package, so no additional packages need to be loaded.
We also use the
select function from the
dplyr package (which is loaded with
tidyverse) to select which variable in the
students tibble we wish to summarize.
The use of the “%>%” syntax is described in a later tutorial.
<- students %>% summary.height select(height_cm) %>% summarysummary.height
## height_cm ## Min. :150.0 ## 1st Qu.:165.0 ## Median :171.5 ## Mean :172.0 ## 3rd Qu.:180.0 ## Max. :210.8
As shown in the output above, the minimum height was 150.0 cm and the maximum student height was 210.8 cm.
TIP: You’ll note that functions and package names above are highlighted in grey. When writing in markdown, it’s good practice to encompass function names and package names in single backticks, i.e.
`tidyverse`. Backticks are typically located with the tilden (“~”) key on your keyboard.