The goal of the mdsr2exercises packages is to provide exercises for the 2nd edition of Modern Data Science with R.
Install from GitHub:
#install.packages("remotes")
remotes::install_github("beanumber/etude")
remotes::install_github("mdsr-book/mdsr2exercises")
This package contains the following set of exercises:
library(mdsr2exercises)
library(dplyr)
library(tidyr)
summary_table <- mdsr_exercise_all() %>%
group_by(chapter, difficulty) %>%
filter(!grepl("onlineonly", tags)) %>%
count() %>%
pivot_wider(names_from = difficulty, values_from = n) %>%
rowwise() %>%
mutate(N = sum(c_across(where(is.numeric)), na.rm = TRUE)) %>%
arrange(desc(N)) %>%
relocate(chapter, Easy, Medium) %>%
janitor::adorn_totals("row")
knitr::kable(summary_table)
chapter | Easy | Medium | Hard | N |
---|---|---|---|---|
data-I | 6 | 8 | 1 | 15 |
appR | 11 | 2 | NA | 13 |
reproducible | 10 | 2 | NA | 12 |
text | 6 | 5 | 1 | 12 |
data-II | 3 | 8 | NA | 11 |
sqlI | 4 | 6 | 1 | 11 |
dataviz-II | 2 | 7 | 1 | 10 |
dataviz-III | 1 | 7 | 2 | 10 |
ethics | 1 | 8 | 1 | 10 |
simulation | NA | 9 | NA | 9 |
algorithmic | 1 | 6 | 1 | 8 |
sqlII | 1 | 2 | 5 | 8 |
foundations | 2 | 5 | NA | 7 |
learning-II | NA | 4 | 3 | 7 |
dataviz-I | 1 | 5 | NA | 6 |
iteration | 2 | 3 | 1 | 6 |
join | 1 | 4 | 1 | 6 |
learning-I | 1 | 4 | 1 | 6 |
modeling | 2 | 3 | 1 | 6 |
regression | 1 | 4 | NA | 5 |
spatial-I | 1 | 1 | 3 | 5 |
spatial-II | NA | 2 | 2 | 4 |
netsci | NA | 1 | 2 | 3 |
not working | NA | 1 | NA | 1 |
Total | 57 | 107 | 27 | 191 |
Here is the summary of the online only exercises:
summary_table <- mdsr_exercise_all() %>%
group_by(chapter, difficulty) %>%
filter(grepl("onlineonly", tags)) %>%
count() %>%
pivot_wider(names_from = difficulty, values_from = n) %>%
rowwise() %>%
mutate(N = sum(c_across(where(is.numeric)), na.rm = TRUE)) %>%
arrange(desc(N)) %>%
relocate(chapter, Easy, Medium) %>%
janitor::adorn_totals("row")
knitr::kable(summary_table)
chapter | Easy | Medium | Hard | N |
---|---|---|---|---|
data-I | 6 | NA | NA | 6 |
dataviz-I | 3 | 2 | NA | 5 |
dataviz-II | 4 | NA | 1 | 5 |
join | 3 | NA | 1 | 4 |
algorithmic | 2 | 1 | NA | 3 |
data-II | 3 | NA | NA | 3 |
text | NA | 2 | 1 | 3 |
dataI | 1 | NA | 1 | 2 |
foundations | NA | 2 | NA | 2 |
simulation | NA | 1 | 1 | 2 |
sqlII | 1 | NA | 1 | 2 |
dataviz-III | NA | 1 | NA | 1 |
ethics | NA | 1 | NA | 1 |
not-working | NA | NA | 1 | 1 |
regression | NA | 1 | NA | 1 |
spatial | NA | NA | 1 | 1 |
sqlI | 1 | NA | NA | 1 |
Total | 24 | 11 | 8 | 43 |
We can build a tibble
of exercises that you want to show. Also specify
any options, as documented in etude::etude_list()
. Here, we show how
to display the first two exercises associated with a particular chapter.
exercises <- mdsr_exercise_ls(chapter == "data-I") %>%
select(-author, -date) %>%
head(2)
knitr::kable(exercises)
chapter | difficulty | version | tags | id | status | file | output | depends | editor_options |
---|---|---|---|---|---|---|---|---|---|
data-I | Easy | 0.1 | onlineonly:::dplyr | cow-make-vase | OK | /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/mdsr2exercises/Exercises/cow-make-vase.Rmd | NA | NA | NA |
data-I | Easy | 0.1 | babynames:::join:::dplyr | crow-burn-sheet | OK | /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/mdsr2exercises/Exercises/crow-burn-sheet.Rmd | NA | NA | NA |
Second, give the in-line command knit_mdsr_exercises(exercises)
:
Problem 1 (Easy): Which dplyr
operation is depicted below?
Problem 2 (Easy): Here is a random subset of the babynames
data
frame in the babynames
package:
Random_subset
#> # A tibble: 10 × 5
#> year sex name n prop
#> <dbl> <chr> <chr> <int> <dbl>
#> 1 2003 M Bilal 146 0.0000695
#> 2 1999 F Terria 23 0.0000118
#> 3 2010 F Naziyah 45 0.0000230
#> 4 1989 F Shawana 41 0.0000206
#> 5 1989 F Jessi 210 0.000105
#> 6 1928 M Tillman 43 0.0000377
#> 7 1981 F Leslee 83 0.0000464
#> 8 1981 F Sherise 27 0.0000151
#> 9 1920 F Marquerite 26 0.0000209
#> 10 1941 M Lorraine 24 0.0000191
For each of the following tables wrangled from Random_subset
, figure
out what dplyr
wrangling statement will produce the result.
- Hint: Both rows and variables are missing from the original
#> # A tibble: 4 × 4
#> year sex name n
#> <dbl> <chr> <chr> <int>
#> 1 2010 F Naziyah 45
#> 2 1989 F Shawana 41
#> 3 1928 M Tillman 43
#> 4 1981 F Leslee 83
- Hint: the
nchar()
function is used in the statement.
#> # A tibble: 2 × 5
#> year sex name n prop
#> <dbl> <chr> <chr> <int> <dbl>
#> 1 1999 F Terria 23 0.0000118
#> 2 1981 F Leslee 83 0.0000464
- Hint: Note the new column, which is constructed from
n
andprop
.
#> # A tibble: 2 × 6
#> year sex name n prop total
#> <dbl> <chr> <chr> <int> <dbl> <dbl>
#> 1 1989 F Shawana 41 0.0000206 1992225.
#> 2 1989 F Jessi 210 0.000105 1991843.
- Hint: All the years are still there, but there are only 8 rows as opposed to the original 10 rows.
#> # A tibble: 8 × 2
#> year total
#> <dbl> <int>
#> 1 1920 26
#> 2 1928 43
#> 3 1941 24
#> 4 1981 110
#> 5 1989 251
#> 6 1999 23
#> 7 2003 146
#> 8 2010 45
To display answers, add the show_answers = TRUE
option.
Exercises are stored in inst/Exercises
. To create a new exercise, call
mdsr_exercise_new()
.
Please also see the etude template vignette
mdsr2exercises also contains quiz and exam questions in the exams format.
The write_moodle()
function wraps exams::exams2moodle()
to create
XML files that can be imported into Moodle.
write_moodle(
pattern = "wrangling",
quiz_name = "wrangling",
dir = tempdir()
)
citation(package = "mdsr2exercises")
#> To cite package 'mdsr2exercises' in publications use:
#>
#> Baumer B, Horton N, Kaplan D (2023). _mdsr2exercises: Exercises for
#> the 2nd edition of MDSR_. R package version 0.7.3.9018.
#>
#> A BibTeX entry for LaTeX users is
#>
#> @Manual{,
#> title = {mdsr2exercises: Exercises for the 2nd edition of MDSR},
#> author = {Benjamin S. Baumer and Nicholas Horton and Daniel Kaplan},
#> year = {2023},
#> note = {R package version 0.7.3.9018},
#> }
The file was last updated Mon Dec 4 08:35:13 2023 GMT.