Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix example tutorials #651

Merged
merged 2 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 26 additions & 34 deletions inst/tutorials/ex-data-basics/ex-data-basics.Rmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "Data basics"
output:
output:
learnr::tutorial:
progressive: true
allow_skip: true
Expand All @@ -20,25 +20,25 @@ tutorial_options(exercise.timelimit = 60)

## Welcome

In this tutorial, you will learn how to use R to inspect the contents of a data frame or tibble. Data frames and tibbles are R's structures for storing tabular data; if you inherit a tabular dataset in R, it will almost certainly come as one of these structures.
In this tutorial, you will learn how to use R to inspect the contents of a data frame or tibble. Data frames and tibbles are R's structures for storing tabular data; if you inherit a tabular dataset in R, it will almost certainly come as one of these structures.

Here, you will learn how to do three things with data frames and tibbles:

1. Look at the contents of a data frame or tibble
2. Open a help page that describes a data frame or tibble
3. Identify the variables and their types in a tibble
3. Identify the variables and their types in a tibble

You will also meet the `mpg` and `flights` datasets. These datasets appear frequently in R examples.
You will also meet the `mpg` and `flights` datasets. These datasets appear frequently in R examples.

The readings in this tutorial follow [_R for Data Science_](http://r4ds.had.co.nz/), sections 3.2 and 5.1.
The readings in this tutorial follow [_R for Data Science_](http://r4ds.had.co.nz/), sections 3.2 and 5.1.

## Data frames

### What is a data frame?

A __data frame__ is a rectangular collection of values, usually organized so that variables appear in the columns and observations appear in rows.
A __data frame__ is a rectangular collection of values, usually organized so that variables appear in the columns and observations appear in rows.

Here is an example: the `mpg` data frame contains observations collected by the US Environmental Protection Agency on 38 models of cars. To see the `mpg` data frame, type `mpg` in the code chunk below and then click "Submit Answer."
Here is an example: the `mpg` data frame contains observations collected by the US Environmental Protection Agency on 38 models of cars. To see the `mpg` data frame, type `mpg` in the code chunk below and then click "Run Code".

```{r mpg-setup}
mpg <- as.data.frame(mpg)
Expand All @@ -49,47 +49,39 @@ mpg <- as.data.frame(mpg)
```

<div id="mpg-hint">
**Hint:** Type `mpg` and then click the Submit Answer button.
**Hint:** Type `mpg` and then click the Run Code button.
</div>

```{r mpg-check, echo = FALSE}
# checking code
```

### A note about mpg

The code above worked because I've already loaded the ggplot2 package for you in this tutorial: `mpg` comes in the ggplot2 package. If you would like to look at `mpg` on your own computer, you will need to first load ggplot2. You can do that in two steps:

1. Run `install.packages('ggplot2')` to install ggplot2 if you do not yet have it.
2. Load ggplot2 with the `library(ggplot2)` command

After that, you will be able to access any object in ggplot2—including `mpg`—until you close R.
After that, you will be able to access any object in ggplot2—including `mpg`—until you close R.

###
###

Did you notice how much information was inside `mpg`? Me too. Sometimes the contents of a data frame do not fit on a single screen, which makes them difficult to inspect. We'll look at an alternative to using and examining data frames soon. But first let's get some help...

## Help pages

### How to open a help page

You can learn more about `mpg` by opening its help page. The help page will explain where the `mpg`dataset comes from and what each variable in `mpg` describes. To open the help page, type `?mpg` in the code chunk below and then click "Submit Answer".
You can learn more about `mpg` by opening its help page. The help page will explain where the `mpg`dataset comes from and what each variable in `mpg` describes. To open the help page, type `?mpg` in the code chunk below and then click "Run Code".

```{r help, exercise = TRUE}

```

<div id="help-hint">
**Hint:** Type `?mpg` and then click the Submit Answer button.
**Hint:** Type `?mpg` and then click the Run Code button.
</div>

```{r help-check, echo = FALSE}
# checking code
```

### ? syntax

You can open a help page for any object that comes with R or with an R package. To open the help page, type a `?` before the object's name and then run the command, as you did with `?mpg`. This technique works for functions, packages, and more.
You can open a help page for any object that comes with R or with an R package. To open the help page, type a `?` before the object's name and then run the command, as you did with `?mpg`. This technique works for functions, packages, and more.

Notice that objects created by you or your colleagues will not have a help page (unless you make one).

Expand All @@ -104,26 +96,26 @@ Use the code chunk below to answer the following questions.
```{r quiz1, echo = FALSE}
quiz(caption = "Quiz",
question("What does the `drv` variable of `mpg` describe? Read the help for `?mpg` to find out.",
answer("Whether or not the vehicle has driver side airbags"),
answer("Whether or not the vehicle has driver side airbags"),
answer("Whether a car is automatic or manual transmission"),
answer("The number of cylinders in the car's engine"),
answer("Something else", correct = TRUE, message = "`drv` describes the type of drivetrain in a car: front wheel drive, rear wheel drive, or four wheel drive."),
answer("Something else", correct = TRUE, message = "`drv` describes the type of drivetrain in a car: front wheel drive, rear wheel drive, or four wheel drive."),
allow_retry = TRUE
),
question("How many rows are in the data frame named `cars`?",
answer("2"),
answer("2"),
answer("25"),
answer("50", correct = TRUE),
answer("100"),
incorrect = "Incorrect.\nHint: R numbers the rows of a data frame when it displays the contents of a data frame. As a result, you can spot the number of rows in `cars` by examining `cars` in the code block above.",
incorrect = "Incorrect.\nHint: R numbers the rows of a data frame when it displays the contents of a data frame. As a result, you can spot the number of rows in `cars` by examining `cars` in the code block above.",
allow_retry = TRUE
),
question("How many columns are in the data frame named `cars`?",
answer("1"),
answer("1"),
answer("2", correct = TRUE),
answer("4"),
answer("more than four"),
incorrect = "Incorrect.\nHint: If you inspect the contents of `cars` in the code block above, it should be pretty easy to count the number of columns.",
incorrect = "Incorrect.\nHint: If you inspect the contents of `cars` in the code block above, it should be pretty easy to count the number of columns.",
allow_retry = TRUE
)
)
Expand All @@ -133,7 +125,7 @@ quiz(caption = "Quiz",

### What is a tibble?

The `flights` data frame in the nycflights13 package is an example of a _tibble_. Tibbles are a data frames with some extra properties.
The `flights` data frame in the nycflights13 package is an example of a _tibble_. Tibbles are a data frames with some extra properties.

To see what I mean, use the code chunk below to print the contents of `flights`.

Expand All @@ -142,10 +134,10 @@ To see what I mean, use the code chunk below to print the contents of `flights`.
```

<div id="flights-hint">
**Hint:** Type the name of the data frame that you want to print and then click the Submit Answer button. I've already loaded the nycflight13 package for you.
**Hint:** Type the name of the data frame that you want to print and then click the Run Code button. I've already loaded the nycflight13 package for you.
</div>

###
###

Good Job. `flights` describes every flight that departed from New York City in 2013. The data comes from the [US Bureau of Transportation Statistics](http://www.transtats.bts.gov/DatabaseInfo.asp?DB_ID=120&Link=0), and is documented in `?flights`.

Expand All @@ -156,17 +148,17 @@ You might notice that `flights` looks a little differently than `mpg`. `flights`

`flights` prints differently because it's a __tibble__. Tibbles are data frames that are slightly tweaked to be more user-friendly. For example, R doesn't try to show you all of a tibble at once (but it will try to show you all of a data frame that is not a tibble).

You can use `as_tibble()` to return a tibble version of any data frame. For example, this would return a tibble version of `mpg`: `as_tibble(mpg)`.
You can use `as_tibble()` to return a tibble version of any data frame. For example, this would return a tibble version of `mpg`: `as_tibble(mpg)`.


## Data types

### Type codes

```{r flights3, echo = FALSE}
flights
```
```

Did you notice that a row of three (or four) letter abbreviations appears under the column names of `flights`? These abbreviations describe the _type_ of data that is stored in each column of `flights`:

* `int` stands for integers.
Expand Down
Loading