diff --git a/content/course.Rmd b/content/course.Rmd index 143e6cc8..14b6eca0 100644 --- a/content/course.Rmd +++ b/content/course.Rmd @@ -73,7 +73,7 @@ Options for working on these worksheets: *Submit the worksheets by uploading your .ipynb file to the corresponding canvas assignment slot*. -*To get full marks for each participation worksheet, you must successfully answer at least 50% of all autograded questions.* +*To get full marks for each formative worksheet, you must successfully answer at least 50% of all autograded questions.* ### Part A diff --git a/content/notes/notes-a02.Rmd b/content/notes/notes-a02.Rmd index bcfc2833..9f01cc1b 100644 --- a/content/notes/notes-a02.Rmd +++ b/content/notes/notes-a02.Rmd @@ -43,12 +43,6 @@ Some additional resources that you might find useful: - Read the [Understanding the GitHub flow](https://guides.github.com/introduction/flow/) to get a sense of branching. -## Test Your Understanding - -1. You've just successfully pushed your branch to GitHub! True or False: it's still possible for the branch on GitHub to contain work that's not on your computer. -2. You're ready to push some code you added to `code.R` to GitHub, but just found out that your teammate completely changed `code.R` on GitHub! True or False: If you pull the repository from GitHub, you'll get an error because your `code.R` conflicts with GitHub's `code.R`. -3. True or False: If you've just changed a file on your computer, in order to push it to GitHub, you'll first need to commit the file. - @@ -142,6 +136,12 @@ STAT 777 @ UBC 4. Save and commit the file. (An informative message here might be "Fixing a merge conflict.") You should now be able to successfully push! +## Test Your Understanding + +1. You've just successfully pushed your branch to GitHub! True or False: it's still possible for the branch on GitHub to contain work that's not on your computer. +2. You're ready to push some code you added to `code.R` to GitHub, but just found out that your teammate completely changed `code.R` on GitHub! True or False: If you pull the repository from GitHub, you'll get an error because your `code.R` conflicts with GitHub's `code.R`. +3. True or False: If you've just changed a file on your computer, in order to push it to GitHub, you'll first need to commit the file. + --- ### Your turn: Branching, merging, and pull requests diff --git a/content/notes/notes-a02.html b/content/notes/notes-a02.html index 3d1cfad1..19e35840 100644 --- a/content/notes/notes-a02.html +++ b/content/notes/notes-a02.html @@ -39,14 +39,6 @@

Resources

- -
-

Test Your Understanding

-
    -
  1. You’ve just successfully pushed your branch to GitHub! True or False: it’s still possible for the branch on GitHub to contain work that’s not on your computer.
  2. -
  3. You’re ready to push some code you added to code.R to GitHub, but just found out that your teammate completely changed code.R on GitHub! True or False: If you pull the repository from GitHub, you’ll get an error because your code.R conflicts with GitHub’s code.R.
  4. -
  5. True or False: If you’ve just changed a file on your computer, in order to push it to GitHub, you’ll first need to commit the file.
  6. -
@@ -124,6 +116,14 @@

Instructor demo: merge conflicts

  • The stuff below the <<<<<<< and above the ======= is what was in your local version; the stuff above the >>>>>>> and below the ======= is what was in the remote conflicting version. Decide what you want to have on the offending line (e.g. “STAT 555 @ UBC), and replace the whole block of text above with that.

  • Save and commit the file. (An informative message here might be “Fixing a merge conflict.”) You should now be able to successfully push!

  • +
    +
    +

    Test Your Understanding

    +
      +
    1. You’ve just successfully pushed your branch to GitHub! True or False: it’s still possible for the branch on GitHub to contain work that’s not on your computer.
    2. +
    3. You’re ready to push some code you added to code.R to GitHub, but just found out that your teammate completely changed code.R on GitHub! True or False: If you pull the repository from GitHub, you’ll get an error because your code.R conflicts with GitHub’s code.R.
    4. +
    5. True or False: If you’ve just changed a file on your computer, in order to push it to GitHub, you’ll first need to commit the file.
    6. +

    Your turn: Branching, merging, and pull requests

    diff --git a/content/notes/notes-a03/index.Rmd b/content/notes/notes-a03/index.Rmd index b18a20df..9a34f9c0 100644 --- a/content/notes/notes-a03/index.Rmd +++ b/content/notes/notes-a03/index.Rmd @@ -50,11 +50,6 @@ Others: - An entertaining and concise argument against using "WYSIWYG" editors like Microsoft Word. -## Test Your Understanding - -1. True or False: It doesn't make sense to output an .Rmd file to markdown, because .Rmd is already markdown. -2. Which of these file formats are proprietary? (a) `.docx`; (b) `.html`; (c) `.txt`; (d) `.Rmd`; (e) `.xlsx` - ## Reproducible data analysis Let's say we have some data analysis results, like a figure or a table. We would say these results are *reproducible* if we could produce the same results by using the same data and code. @@ -175,6 +170,11 @@ Just like YAML is metadata for the Rmd document, *code chunk options* are metada 3. Knit the results. Can you spot the differences? +## Test Your Understanding + +1. True or False: It doesn't make sense to output an .Rmd file to markdown, because .Rmd is already markdown. +2. Which of these file formats are proprietary? (a) `.docx`; (b) `.html`; (c) `.txt`; (d) `.Rmd`; (e) `.xlsx` + --- ### Your turn: data update! diff --git a/content/notes/notes-a03/index.html b/content/notes/notes-a03/index.html index 97699531..a7720977 100644 --- a/content/notes/notes-a03/index.html +++ b/content/notes/notes-a03/index.html @@ -48,13 +48,6 @@

    Resources

    -
    -

    Test Your Understanding

    -
      -
    1. True or False: It doesn’t make sense to output an .Rmd file to markdown, because .Rmd is already markdown.
    2. -
    3. Which of these file formats are proprietary? (a) .docx; (b) .html; (c) .txt; (d) .Rmd; (e) .xlsx
    4. -
    -

    Reproducible data analysis

    Let’s say we have some data analysis results, like a figure or a table. We would say these results are reproducible if we could produce the same results by using the same data and code.

    @@ -147,9 +140,16 @@

    Exploring chunk options

  • Change the figure width and height with fig.width = 5 and fig.height = 3.

  • Knit the results. Can you spot the differences?

  • -
    + +
    +

    Test Your Understanding

    +
      +
    1. True or False: It doesn’t make sense to output an .Rmd file to markdown, because .Rmd is already markdown.
    2. +
    3. Which of these file formats are proprietary? (a) .docx; (b) .html; (c) .txt; (d) .Rmd; (e) .xlsx
    4. +
    +

    Your turn: data update!

    In the instructor demo, we prettied up a nice report about mtcars together. But suddenly, you receive an email from your collaborator who collected the mtcars data, who says that the data on Mazda RX4, the Valiant, and the Volvo 142E were transcribed wrong from the magazine! They’re going to work on correcting them, but in the mean time, they’d like to re-run the analysis with those three cars excluded.

    diff --git a/content/notes/notes-a04.Rmd b/content/notes/notes-a04.Rmd index fb4aa82d..0bbd23a0 100644 --- a/content/notes/notes-a04.Rmd +++ b/content/notes/notes-a04.Rmd @@ -32,13 +32,6 @@ Optional further reading to learn about the `across()` function: - [Hadley Wickham's blog post on the topic](https://www.tidyverse.org/blog/2020/04/dplyr-1-0-0-colwise/). -## Test Your Understanding - -1. True or False? Running filter or select on your tibble permanently changes your tibble. -2. True or False? The pipe operator `%>%` works for functions outside of the tidyverse, too. -3. True or False? `summarise()` reduces each group down to one row, and `mutate()` preserves the number of rows. But, between the two, there's a way to reduce each group down to _two_ rows. -4. True or False? `across()` doesn't allow you to make new column names. - ## Why Data Manipulation? You have a shiny new data set - great! You might be tempted to dive right in and start making pretty graphs and fitting cool models to your data. Not so fast! In practice, it's very rare that you have the data in the exact right form to make the graph you want, or fit the model you want. You will need to start by *manipulating* your data into the right form: creating new variables, subsetting rows, renaming columns, etc. @@ -53,6 +46,15 @@ An even less sexy but perhaps even more important part of data analysis is simpl All of these require data manipulation. We choose to use the `dplyr` (pronounced d-plier) package for this. +## Test Your Understanding + +1. True or False? Running filter or select on your tibble permanently changes your tibble. +2. True or False? The pipe operator `%>%` works for functions outside of the tidyverse, too. +3. True or False? `summarise()` reduces each group down to one row, and `mutate()` preserves the number of rows. But, between the two, there's a way to reduce each group down to _two_ rows. +4. True or False? `across()` doesn't allow you to make new column names. + +--- + ## Your turn: learning to use dplyr We think the best way to learn the basics of dplyr is to work through Worksheet A2. diff --git a/content/notes/notes-a04.html b/content/notes/notes-a04.html index c9bf3e82..bfb13828 100644 --- a/content/notes/notes-a04.html +++ b/content/notes/notes-a04.html @@ -37,15 +37,6 @@

    Resources

  • Hadley Wickham’s blog post on the topic.
  • -
    -

    Test Your Understanding

    -
      -
    1. True or False? Running filter or select on your tibble permanently changes your tibble.
    2. -
    3. True or False? The pipe operator %>% works for functions outside of the tidyverse, too.
    4. -
    5. True or False? summarise() reduces each group down to one row, and mutate() preserves the number of rows. But, between the two, there’s a way to reduce each group down to two rows.
    6. -
    7. True or False? across() doesn’t allow you to make new column names.
    8. -
    -

    Why Data Manipulation?

    You have a shiny new data set - great! You might be tempted to dive right in and start making pretty graphs and fitting cool models to your data. Not so fast! In practice, it’s very rare that you have the data in the exact right form to make the graph you want, or fit the model you want. You will need to start by manipulating your data into the right form: creating new variables, subsetting rows, renaming columns, etc.

    @@ -58,6 +49,16 @@

    Why Data Manipulation?

    All of these require data manipulation. We choose to use the dplyr (pronounced d-plier) package for this.

    +
    +

    Test Your Understanding

    +
      +
    1. True or False? Running filter or select on your tibble permanently changes your tibble.
    2. +
    3. True or False? The pipe operator %>% works for functions outside of the tidyverse, too.
    4. +
    5. True or False? summarise() reduces each group down to one row, and mutate() preserves the number of rows. But, between the two, there’s a way to reduce each group down to two rows.
    6. +
    7. True or False? across() doesn’t allow you to make new column names.
    8. +
    +
    +

    Your turn: learning to use dplyr

    We think the best way to learn the basics of dplyr is to work through Worksheet A2.

    diff --git a/content/notes/notes-a06.Rmd b/content/notes/notes-a06.Rmd index 0b3f168d..9762fe73 100644 --- a/content/notes/notes-a06.Rmd +++ b/content/notes/notes-a06.Rmd @@ -59,13 +59,6 @@ Resources about producing effective visualizations: - Vincenzo's ["Communicating data" slides](https://vincenzocoia.com/communicating_data/#1) provide some rules of thumb. - [Callingbull.org's entry on visualizations](https://www.callingbull.org/tools/tools_misleading_axes.html), by Carl T. Bergstrom and Jevin West, goes over several examples of improving ineffective visuals. -## Test Your Understanding - -1. True or False: You can re-specify global aesthetics by adding an `aes()` layer to a ggplot object. -2. True or False: With ggplot2, we can specify the data in a geom layer, in addition to the initial `ggplot()` layer. -3. True or False: Because the `dplyr::group_by()` function and ggplot2's `group` aesthetic both split a tibble into groups, we can use either one when making a plot. -4. True or False: In principle, mapping `continent` to letters (a, b, c, ...) is an aesthetic mapping. - ## Why Plotting? The human visual cortex is a powerful thing. If you're wanting to point someone's attention to a bunch of numbers, I can assure you that you won't elicit any "aha" moments by displaying those numbers in a table, either in a report or (especially!) a presentation. Turn those numbers into a plot, and suddenly you can see patterns and relationships. @@ -182,6 +175,15 @@ ggplot(gapminder, aes(gdpPercap, lifeExp)) + ylab("Life Expectancy") ``` +## Test Your Understanding + +1. True or False: You can re-specify global aesthetics by adding an `aes()` layer to a ggplot object. +2. True or False: With ggplot2, we can specify the data in a geom layer, in addition to the initial `ggplot()` layer. +3. True or False: Because the `dplyr::group_by()` function and ggplot2's `group` aesthetic both split a tibble into groups, we can use either one when making a plot. +4. True or False: In principle, mapping `continent` to letters (a, b, c, ...) is an aesthetic mapping. + +--- + ## Your turn: learning to use ggplot2 We think the best way to learn the basics of ggplot2 is to work through Worksheet A3. diff --git a/content/notes/notes-a06.html b/content/notes/notes-a06.html index 711e8767..66451ca0 100644 --- a/content/notes/notes-a06.html +++ b/content/notes/notes-a06.html @@ -58,15 +58,6 @@

    Resources

    -
    -

    Test Your Understanding

    -
      -
    1. True or False: You can re-specify global aesthetics by adding an aes() layer to a ggplot object.
    2. -
    3. True or False: With ggplot2, we can specify the data in a geom layer, in addition to the initial ggplot() layer.
    4. -
    5. True or False: Because the dplyr::group_by() function and ggplot2’s group aesthetic both split a tibble into groups, we can use either one when making a plot.
    6. -
    7. True or False: In principle, mapping continent to letters (a, b, c, …) is an aesthetic mapping.
    8. -
    -

    Why Plotting?

    The human visual cortex is a powerful thing. If you’re wanting to point someone’s attention to a bunch of numbers, I can assure you that you won’t elicit any “aha” moments by displaying those numbers in a table, either in a report or (especially!) a presentation. Turn those numbers into a plot, and suddenly you can see patterns and relationships.

    @@ -127,37 +118,37 @@

    Example: Scatterplot grammar

    This scatterplot has the following grammar components:

    - + - + - + - + - + - + - + - + @@ -198,6 +189,16 @@

    Example: your first ggplot2 plot

    ylab("Life Expectancy")

    +
    +

    Test Your Understanding

    +
      +
    1. True or False: You can re-specify global aesthetics by adding an aes() layer to a ggplot object.
    2. +
    3. True or False: With ggplot2, we can specify the data in a geom layer, in addition to the initial ggplot() layer.
    4. +
    5. True or False: Because the dplyr::group_by() function and ggplot2’s group aesthetic both split a tibble into groups, we can use either one when making a plot.
    6. +
    7. True or False: In principle, mapping continent to letters (a, b, c, …) is an aesthetic mapping.
    8. +
    +
    +

    Your turn: learning to use ggplot2

    We think the best way to learn the basics of ggplot2 is to work through Worksheet A3.

    @@ -240,7 +241,7 @@

    Class 2: FEV Case Study

    Example: -```r +``` r ggplot(gapminder, aes(lifeExp)) + geom_histogram(bins = 50) ``` @@ -263,7 +264,7 @@

    Class 2: FEV Case Study

    Example: -```r +``` r ggplot(gapminder, aes(lifeExp)) + geom_density() ``` @@ -282,7 +283,7 @@

    Class 2: FEV Case Study

    Example: -```r +``` r ggplot(gapminder, aes(continent, lifeExp)) + geom_jitter() ``` @@ -301,7 +302,7 @@

    Class 2: FEV Case Study

    Example: -```r +``` r ggplot(gapminder, aes(continent, lifeExp)) + geom_boxplot() ``` @@ -323,7 +324,7 @@

    Class 2: FEV Case Study

    Example: -```r +``` r ggplot(gapminder, aes(lifeExp, continent)) + ggridges::geom_density_ridges() ``` @@ -351,7 +352,7 @@

    Class 2: FEV Case Study

    Example: number of 4-, 6-, and 8- cylinder cars in the `mtcars` dataset: -```r +``` r ggplot(mtcars, aes(cyl)) + geom_bar() ``` @@ -370,7 +371,7 @@

    Class 2: FEV Case Study

    Although not required, the `group` aesthetic will come in handy here. This aesthetic produces a plot independently for each group, and overlays the results. -```r +``` r tsibble::as_tsibble(co2) %>% rename(yearmonth = index, conc = value) %>% @@ -381,6 +382,12 @@

    Class 2: FEV Case Study

    ylab("CO2 Concentration") ``` +``` +## Registered S3 method overwritten by 'tsibble': +## method from +## as_tibble.grouped_df dplyr +``` + diff --git a/content/notes/notes-a09.Rmd b/content/notes/notes-a09.Rmd index efa969da..e3da2a06 100644 --- a/content/notes/notes-a09.Rmd +++ b/content/notes/notes-a09.Rmd @@ -37,13 +37,6 @@ Other resources, in addition to the notes below: - If you're eager to learn more about models in general, [An Introduction to Statistical Learning](https://www-bcf.usc.edu/~gareth/ISL/) is an approachable read (the book is freely available online) - [Mike Marin's R playlist on YouTube](https://www.youtube.com/playlist?list=PLqzoL9-eJTNBlVXxWvJkq0dtVut2sICUW) helps you use R to obtain statistical results. -## Test Your Understanding - -1. TRUE or FALSE: the output of `glance` on the above returns only 1 row because there is only 1 explanatory variable in the model. -2. TRUE or FALSE: the output of `broom::tidy()` package is more "tidy" than `coef(summary())` because the output is a `tibble` -3. TRUE or FALSE: We need to use a modelling function, such as `lm`, before we can graph a fitted line with `geom_smooth`. -4. TRUE or FALSE: If I wanted to find the amount that `lifeExp` changes per `year`, then I need to use `broom::tidy()` - # Model-fitting in R Data wrangling and plotting can get you pretty far when it comes to drawing insight from your data. But, sometimes you need to go further. For example: @@ -313,6 +306,15 @@ ggplot(gapminder_Zimbabwe, aes(year,lifeExp)) + 5. `geom_smooth()` - used to add geom_layer that shows a fitted line to the data. `method` and `formula` arguments can be used to customize model. +## Test Your Understanding + +1. TRUE or FALSE: the output of `glance` on the above returns only 1 row because there is only 1 explanatory variable in the model. +2. TRUE or FALSE: the output of `broom::tidy()` package is more "tidy" than `coef(summary())` because the output is a `tibble` +3. TRUE or FALSE: We need to use a modelling function, such as `lm`, before we can graph a fitted line with `geom_smooth`. +4. TRUE or FALSE: If I wanted to find the amount that `lifeExp` changes per `year`, then I need to use `broom::tidy()` + +--- + ## Your turn: learning to fit models in R We think the best way to learn the basics from here is to work through the last part of Worksheet A4. diff --git a/content/notes/notes-a09.html b/content/notes/notes-a09.html index 7640f9a8..c681a8b6 100644 --- a/content/notes/notes-a09.html +++ b/content/notes/notes-a09.html @@ -31,15 +31,6 @@

    Resources

  • Mike Marin’s R playlist on YouTube helps you use R to obtain statistical results.
  • -
    -

    Test Your Understanding

    -
      -
    1. TRUE or FALSE: the output of glance on the above returns only 1 row because there is only 1 explanatory variable in the model.
    2. -
    3. TRUE or FALSE: the output of broom::tidy() package is more “tidy” than coef(summary()) because the output is a tibble
    4. -
    5. TRUE or FALSE: We need to use a modelling function, such as lm, before we can graph a fitted line with geom_smooth.
    6. -
    7. TRUE or FALSE: If I wanted to find the amount that lifeExp changes per year, then I need to use broom::tidy()
    8. -
    -

    Model-fitting in R

    Data wrangling and plotting can get you pretty far when it comes to drawing insight from your data. But, sometimes you need to go further. For example:

    @@ -267,6 +258,16 @@

    Summary

  • geom_smooth() - used to add geom_layer that shows a fitted line to the data. method and formula arguments can be used to customize model.

  • +
    +

    Test Your Understanding

    +
      +
    1. TRUE or FALSE: the output of glance on the above returns only 1 row because there is only 1 explanatory variable in the model.
    2. +
    3. TRUE or FALSE: the output of broom::tidy() package is more “tidy” than coef(summary()) because the output is a tibble
    4. +
    5. TRUE or FALSE: We need to use a modelling function, such as lm, before we can graph a fitted line with geom_smooth.
    6. +
    7. TRUE or FALSE: If I wanted to find the amount that lifeExp changes per year, then I need to use broom::tidy()
    8. +
    +
    +

    Your turn: learning to fit models in R

    We think the best way to learn the basics from here is to work through the last part of Worksheet A4.

    diff --git a/content/notes/notes-a10.Rmd b/content/notes/notes-a10.Rmd index 8713db2d..d72a0986 100644 --- a/content/notes/notes-a10.Rmd +++ b/content/notes/notes-a10.Rmd @@ -33,32 +33,6 @@ Other resources, in addition to the notes below: - The [R4DS](https://r4ds.had.co.nz/dates-and-times.html) chapter on Dates and Times - The `tsibble` [vignette](https://cran.rstudio.com/web/packages/tsibble/vignettes/intro-tsibble.html) to learn more about embedding a time series within a tibble. -## Test Your Understanding - -1. The output of the following code is equivalent to a factor with the letters "a" to "f". - -``` r -(abc <- factor(letters[1:3])) -#> [1] a b c -#> Levels: a b c -(def <- factor(letters[4:6])) -#> [1] d e f -#> Levels: d e f -c(abc, def) -``` - -2. The output of the following code is a date object. - -```r -library(lubridate) - -date <- ymd("2020-10-13") - -dt <- ymd_hms("2020-10-13 09:30:00") - -c(date, dt) -``` - ## Factors > "There is no other object that creates as much trouble as factors." - Patrick Burns, "The R Inferno". @@ -106,6 +80,33 @@ Often you will need to work with dates and times in your data. For example, we c Dates and times seem simple, but they are actually one of the most complicated things you will encounter in data analysis. +## Test Your Understanding + +1. The output of the following code is equivalent to a factor with the letters "a" to "f". + +``` r +(abc <- factor(letters[1:3])) +#> [1] a b c +#> Levels: a b c +(def <- factor(letters[4:6])) +#> [1] d e f +#> Levels: d e f +c(abc, def) +``` + +2. The output of the following code is a date object. + +```r +library(lubridate) + +date <- ymd("2020-10-13") + +dt <- ymd_hms("2020-10-13 09:30:00") + +c(date, dt) +``` + + -------------- ## Your turn: Making a date variable diff --git a/content/notes/notes-a10.html b/content/notes/notes-a10.html index b81ef03e..f8f82885 100644 --- a/content/notes/notes-a10.html +++ b/content/notes/notes-a10.html @@ -30,29 +30,6 @@

    Resources

    -
    -

    Test Your Understanding

    -
      -
    1. The output of the following code is equivalent to a factor with the letters “a” to “f”.
    2. -
    -
    (abc <- factor(letters[1:3]))
    -#> [1] a b c
    -#> Levels: a b c
    -(def <- factor(letters[4:6]))
    -#> [1] d e f
    -#> Levels: d e f
    -c(abc, def)
    -
      -
    1. The output of the following code is a date object.
    2. -
    -
    library(lubridate)
    -
    -date <- ymd("2020-10-13")
    -
    -dt <- ymd_hms("2020-10-13 09:30:00")
    -
    -c(date, dt)
    -

    Factors

    @@ -73,7 +50,8 @@

    Factors

    Under the hood, R stores a factor with (say) 3 levels as a numeric vector containing integers between 1 and 3, paired with a character vector of length 3 that identifies the mapping between the numbers 1, 2, and 3 and the levels.

    This is not immediately obvious, because R will print factors using the character string levels rather than the numbers that it stores:

    penguins %>% slice_sample(n=10) %>% pull(species)
    -
    ##  [1] Adelie Adelie Gentoo Gentoo Gentoo Gentoo Adelie Gentoo Adelie Adelie
    +
    ##  [1] Gentoo    Adelie    Adelie    Gentoo    Gentoo    Adelie    Adelie   
    +##  [8] Chinstrap Adelie    Adelie   
     ## Levels: Adelie Chinstrap Gentoo

    This dual nature of factors creates a whole slew of hidden traps and headaches, especially for new useRs!

    Nevertheless, factors are important and worth the pain. Many functions throughout the R landscape expect categorical variables to be coded as factors. For example, when making plots in either ggplot2 or in base R, we need factors in order to map categorical variables to aesthetic elements like colour.

    @@ -101,9 +79,32 @@

    Second part

    Third part: Dates and/or Times

    Often you will need to work with dates and times in your data. For example, we could have had a variable in the FEV data set that contains the date of each patient visit.

    Dates and times seem simple, but they are actually one of the most complicated things you will encounter in data analysis.

    -
    +
    +

    Test Your Understanding

    +
      +
    1. The output of the following code is equivalent to a factor with the letters “a” to “f”.
    2. +
    +
    (abc <- factor(letters[1:3]))
    +#> [1] a b c
    +#> Levels: a b c
    +(def <- factor(letters[4:6]))
    +#> [1] d e f
    +#> Levels: d e f
    +c(abc, def)
    +
      +
    1. The output of the following code is a date object.
    2. +
    +
    library(lubridate)
    +
    +date <- ymd("2020-10-13")
    +
    +dt <- ymd_hms("2020-10-13 09:30:00")
    +
    +c(date, dt)
    +
    +

    Your turn: Making a date variable

      diff --git a/content/notes/notes-a12.Rmd b/content/notes/notes-a12.Rmd index bd245647..dd7c6d1c 100644 --- a/content/notes/notes-a12.Rmd +++ b/content/notes/notes-a12.Rmd @@ -21,12 +21,6 @@ Tutorial: - The ["Writing and Reading files"](https://stat545.com/import-export.html) chapter of stat545.com. -## Test your Understanding - -1. True or False: if you want to be deliberate about where `here::here()` points to on your computer, you need to ensure you have an .Rproj file. -2. True or False: Suppose you have an .Rproj file in the same folder as your R script. Running `here::here()` from that R script will always point to that folder. - - ## Formats Here are a few formats that you might want to read and write data to: @@ -52,6 +46,13 @@ Want to read and write to an Excel file? The `readxl` package in the tidyverse i For the very niche option of R binary: `read_rds()` and `write_rds()`. +## Test your Understanding + +1. True or False: if you want to be deliberate about where `here::here()` points to on your computer, you need to ensure you have an .Rproj file. +2. True or False: Suppose you have an .Rproj file in the same folder as your R script. Running `here::here()` from that R script will always point to that folder. + +--- + ## Your turn: try it out! Open RStudio. Go to Session => Set Working Directory => Choose Directory and then pick a folder you would like to read and write data into. Then, run the following piece of code: diff --git a/content/notes/notes-a12.html b/content/notes/notes-a12.html index 77fc9259..a3b4f894 100644 --- a/content/notes/notes-a12.html +++ b/content/notes/notes-a12.html @@ -23,13 +23,6 @@

      Resources

    • The “Writing and Reading files” chapter of stat545.com.
    -
    -

    Test your Understanding

    -
      -
    1. True or False: if you want to be deliberate about where here::here() points to on your computer, you need to ensure you have an .Rproj file.
    2. -
    3. True or False: Suppose you have an .Rproj file in the same folder as your R script. Running here::here() from that R script will always point to that folder.
    4. -
    -

    Formats

    Here are a few formats that you might want to read and write data to:

    @@ -52,6 +45,14 @@

    Reading and writing data in R

    Want to read and write to an Excel file? The readxl package in the tidyverse is for you!

    For the very niche option of R binary: read_rds() and write_rds().

    +
    +

    Test your Understanding

    +
      +
    1. True or False: if you want to be deliberate about where here::here() points to on your computer, you need to ensure you have an .Rproj file.
    2. +
    3. True or False: Suppose you have an .Rproj file in the same folder as your R script. Running here::here() from that R script will always point to that folder.
    4. +
    +
    +

    Your turn: try it out!

    Open RStudio. Go to Session => Set Working Directory => Choose Directory and then pick a folder you would like to read and write data into. Then, run the following piece of code:

    diff --git a/content/notes/notes-b02.Rmd b/content/notes/notes-b02.Rmd index 3495ba0c..ca9fb672 100644 --- a/content/notes/notes-b02.Rmd +++ b/content/notes/notes-b02.Rmd @@ -41,16 +41,6 @@ Written material: - [Package development cheat sheet](https://rawgit.com/rstudio/cheatsheets/master/package-development.pdf) - To learn more about using S3 object oriented functions in your package, see ["Advanced R" Chapter 13: S3](https://adv-r.hadley.nz/s3.html). -## Test Your Understanding - -It might help to browse a completed package directory to answer these questions, like the [powers package](https://github.com/vincenzocoia/powers). - -1. True or False: The mandatory files in an R package are a DESCRIPTION file, a NAMESPACE file, and functions in the R/ directory -- everything else is just helpful. -2. So far we've been using devtools functions in the console (instead of saved in a script). True or False: if you're making a bigger R package, you should start putting those devtools functions in one or more R scripts. -3. True or False: Add any package name to the `Imports` field in the DESCRIPTION file, and we can now use functions from that package using `::`. -4. True or False: We can't *both* add exports to the NAMESPACE file manually and by using `devtools::document()`. - - ## Why R Packages Why make an R package? As mentioned in the "functions" topic, your analysis will probably benefit from homemade functions: making functions forces you to think about your analysis in terms of its key computational parts, and makes for robust and readable code. Here are a few benefits that result by bundling these functions into an R package: @@ -99,4 +89,13 @@ Release your package: - Make a NEWS.md file with `use_news_md()` and add the main development notes. - Choose a version number; put in description file, and tag a release on GitHub. - - You should also prepare your package for the next version -- I suggest doing this on a new branch. \ No newline at end of file + - You should also prepare your package for the next version -- I suggest doing this on a new branch. + +## Test Your Understanding + +It might help to browse a completed package directory to answer these questions, like the [powers package](https://github.com/vincenzocoia/powers). + +1. True or False: The mandatory files in an R package are a DESCRIPTION file, a NAMESPACE file, and functions in the R/ directory -- everything else is just helpful. +2. So far we've been using devtools functions in the console (instead of saved in a script). True or False: if you're making a bigger R package, you should start putting those devtools functions in one or more R scripts. +3. True or False: Add any package name to the `Imports` field in the DESCRIPTION file, and we can now use functions from that package using `::`. +4. True or False: We can't *both* add exports to the NAMESPACE file manually and by using `devtools::document()`. diff --git a/content/notes/notes-b02.html b/content/notes/notes-b02.html index 148b87ab..a769dd23 100644 --- a/content/notes/notes-b02.html +++ b/content/notes/notes-b02.html @@ -43,16 +43,6 @@

    Resources

  • To learn more about using S3 object oriented functions in your package, see “Advanced R” Chapter 13: S3.
  • -
    -

    Test Your Understanding

    -

    It might help to browse a completed package directory to answer these questions, like the powers package.

    -
      -
    1. True or False: The mandatory files in an R package are a DESCRIPTION file, a NAMESPACE file, and functions in the R/ directory – everything else is just helpful.
    2. -
    3. So far we’ve been using devtools functions in the console (instead of saved in a script). True or False: if you’re making a bigger R package, you should start putting those devtools functions in one or more R scripts.
    4. -
    5. True or False: Add any package name to the Imports field in the DESCRIPTION file, and we can now use functions from that package using ::.
    6. -
    7. True or False: We can’t both add exports to the NAMESPACE file manually and by using devtools::document().
    8. -
    -

    Why R Packages

    Why make an R package? As mentioned in the “functions” topic, your analysis will probably benefit from homemade functions: making functions forces you to think about your analysis in terms of its key computational parts, and makes for robust and readable code. Here are a few benefits that result by bundling these functions into an R package:

    @@ -112,3 +102,13 @@

    Agenda

    +
    +

    Test Your Understanding

    +

    It might help to browse a completed package directory to answer these questions, like the powers package.

    +
      +
    1. True or False: The mandatory files in an R package are a DESCRIPTION file, a NAMESPACE file, and functions in the R/ directory – everything else is just helpful.
    2. +
    3. So far we’ve been using devtools functions in the console (instead of saved in a script). True or False: if you’re making a bigger R package, you should start putting those devtools functions in one or more R scripts.
    4. +
    5. True or False: Add any package name to the Imports field in the DESCRIPTION file, and we can now use functions from that package using ::.
    6. +
    7. True or False: We can’t both add exports to the NAMESPACE file manually and by using devtools::document().
    8. +
    +
    diff --git a/content/notes/notes-b03.Rmd b/content/notes/notes-b03.Rmd index cae68bc1..afeb1b9f 100644 --- a/content/notes/notes-b03.Rmd +++ b/content/notes/notes-b03.Rmd @@ -34,19 +34,6 @@ No video lecture for this optional topic. Written material: - -## Test Your Understanding - -Use these questions to check your understanding of the material. - -1. True or False: You've opened Terminal, and are now about to run `Rscript` for a second time. Because you haven't restarted the Terminal, the code will continue to build in the same session as before. -2. You have information in `script1.R` that you'd like to pass to `scipt2.R`. True or false: the best way to pass the info to `script2.R` is by saving the final workspace from `script1.R` in an `.RData` or `.rds` file, and loading that it into `script2.R`. -3. True or False: It's almost always better to write an `.Rmd` file over an `.R` file, because you're able to interlace markdown. -4. True or false: `make`ing a *phony* target will always run its rules, whereas `make`ing a target *file* will only run its rules if the output needs updating. -5. True or false: If a dependency file is not present on your computer, you can still call `make` error-free if the dependency is the target of another rule. This is true even if the dependency never gets made. -6. True or false: The rules to `make` a target file will be run if either the target file or the dependencies are modified. -7. True or false: Dependencies in `make` are accessed "lazily", so that if the dependencies are never actually used when executing a rule, they don't actually have any impact on the Makefile. - ## Why Automation It often makes sense to break up a task (e.g. "analyze data and turn it into publication ready figures and tables") into smaller chunks, e.g. "data cleaning" vs "summarizing and plotting" vs "model fitting". This leads to a *pipeline*: a system where the code for some tasks (e.g. summarizing and plotting) depend on the output of others (e.g. data cleaning). @@ -180,6 +167,20 @@ Once we get there, we'll work through the activity in [stat545.com Chapter 36](h + +## Test Your Understanding + +Use these questions to check your understanding of the material. + +1. True or False: You've opened Terminal, and are now about to run `Rscript` for a second time. Because you haven't restarted the Terminal, the code will continue to build in the same session as before. +2. You have information in `script1.R` that you'd like to pass to `scipt2.R`. True or false: the best way to pass the info to `script2.R` is by saving the final workspace from `script1.R` in an `.RData` or `.rds` file, and loading that it into `script2.R`. +3. True or False: It's almost always better to write an `.Rmd` file over an `.R` file, because you're able to interlace markdown. +4. True or false: `make`ing a *phony* target will always run its rules, whereas `make`ing a target *file* will only run its rules if the output needs updating. +5. True or false: If a dependency file is not present on your computer, you can still call `make` error-free if the dependency is the target of another rule. This is true even if the dependency never gets made. +6. True or false: The rules to `make` a target file will be run if either the target file or the dependencies are modified. +7. True or false: Dependencies in `make` are accessed "lazily", so that if the dependencies are never actually used when executing a rule, they don't actually have any impact on the Makefile. + + #### Attribution Written by Vincenzo Coia, with inspiration from Tiffany Timbers for the explanation of Makefiles, as well as the make activity from Shaun Jackman and Jenny Bryan created for this course prior to 2017. \ No newline at end of file diff --git a/content/notes/notes-b03.html b/content/notes/notes-b03.html index 07dab04b..bb5b784e 100644 --- a/content/notes/notes-b03.html +++ b/content/notes/notes-b03.html @@ -37,19 +37,6 @@

    Resources

    -
    -

    Test Your Understanding

    -

    Use these questions to check your understanding of the material.

    -
      -
    1. True or False: You’ve opened Terminal, and are now about to run Rscript for a second time. Because you haven’t restarted the Terminal, the code will continue to build in the same session as before.
    2. -
    3. You have information in script1.R that you’d like to pass to scipt2.R. True or false: the best way to pass the info to script2.R is by saving the final workspace from script1.R in an .RData or .rds file, and loading that it into script2.R.
    4. -
    5. True or False: It’s almost always better to write an .Rmd file over an .R file, because you’re able to interlace markdown.
    6. -
    7. True or false: makeing a phony target will always run its rules, whereas makeing a target file will only run its rules if the output needs updating.
    8. -
    9. True or false: If a dependency file is not present on your computer, you can still call make error-free if the dependency is the target of another rule. This is true even if the dependency never gets made.
    10. -
    11. True or false: The rules to make a target file will be run if either the target file or the dependencies are modified.
    12. -
    13. True or false: Dependencies in make are accessed “lazily”, so that if the dependencies are never actually used when executing a rule, they don’t actually have any impact on the Makefile.
    14. -
    -

    Why Automation

    It often makes sense to break up a task (e.g. “analyze data and turn it into publication ready figures and tables”) into smaller chunks, e.g. “data cleaning” vs “summarizing and plotting” vs “model fitting”. This leads to a pipeline: a system where the code for some tasks (e.g. summarizing and plotting) depend on the output of others (e.g. data cleaning).

    @@ -139,6 +126,19 @@

    Agenda

    +
    +
    +

    Test Your Understanding

    +

    Use these questions to check your understanding of the material.

    +
      +
    1. True or False: You’ve opened Terminal, and are now about to run Rscript for a second time. Because you haven’t restarted the Terminal, the code will continue to build in the same session as before.
    2. +
    3. You have information in script1.R that you’d like to pass to scipt2.R. True or false: the best way to pass the info to script2.R is by saving the final workspace from script1.R in an .RData or .rds file, and loading that it into script2.R.
    4. +
    5. True or False: It’s almost always better to write an .Rmd file over an .R file, because you’re able to interlace markdown.
    6. +
    7. True or false: makeing a phony target will always run its rules, whereas makeing a target file will only run its rules if the output needs updating.
    8. +
    9. True or false: If a dependency file is not present on your computer, you can still call make error-free if the dependency is the target of another rule. This is true even if the dependency never gets made.
    10. +
    11. True or false: The rules to make a target file will be run if either the target file or the dependencies are modified.
    12. +
    13. True or false: Dependencies in make are accessed “lazily”, so that if the dependencies are never actually used when executing a rule, they don’t actually have any impact on the Makefile.
    14. +

    Attribution

    Written by Vincenzo Coia, with inspiration from Tiffany Timbers for the explanation of Makefiles, as well as the make activity from Shaun Jackman and Jenny Bryan created for this course prior to 2017.

    diff --git a/content/notes/notes-b04.Rmd b/content/notes/notes-b04.Rmd index a0862607..9156bb62 100644 --- a/content/notes/notes-b04.Rmd +++ b/content/notes/notes-b04.Rmd @@ -38,6 +38,10 @@ Other dashboard tools besides shiny (but not in R): - With python: plotly dash. Check out the [main website](https://plotly.com/dash/), or [this Medium post](https://medium.com/plotly/introducing-dash-5ecf7191b503) introducing the tool. - With javascript: [D3](https://d3js.org/), a tremendously powerful tool with a steep learning curve (esp. if you don't know javascript). +## Agenda + +We are going to be working off of a [slide deck](https://docs.google.com/presentation/d/1dXhqqsD7dPOOdcC5Y7RW--dEU7UfU52qlb0YD3kKeLw/edit#slide=id.p) and [tutorial](https://deanattali.com/blog/building-shiny-apps-tutorial/) written by Dean Attali. This should take two classes. + ## Test Your Understanding 1. True or False: A shiny app, as a website, still requires an instance of R running in the background. The stat545.stat.ubc.ca website is static: it doesn't require an active session to be running in the background. @@ -50,9 +54,6 @@ Other dashboard tools besides shiny (but not in R): ``` 4. True or False: Since shiny's UI is html, we can manually write the UI by writing HTML in the `HTML()` function. -## Agenda - -We are going to be working off of a [slide deck](https://docs.google.com/presentation/d/1dXhqqsD7dPOOdcC5Y7RW--dEU7UfU52qlb0YD3kKeLw/edit#slide=id.p) and [tutorial](https://deanattali.com/blog/building-shiny-apps-tutorial/) written by Dean Attali. This should take two classes. diff --git a/content/notes/notes-b04.html b/content/notes/notes-b04.html index 49d853aa..865b150e 100644 --- a/content/notes/notes-b04.html +++ b/content/notes/notes-b04.html @@ -42,6 +42,10 @@

    Resources

  • With javascript: D3, a tremendously powerful tool with a steep learning curve (esp. if you don’t know javascript).
  • +
    +

    Agenda

    +

    We are going to be working off of a slide deck and tutorial written by Dean Attali. This should take two classes.

    +

    Test Your Understanding

      @@ -53,10 +57,6 @@

      Test Your Understanding

      x <- 1
    1. True or False: Since shiny’s UI is html, we can manually write the UI by writing HTML in the HTML() function.

    -
    -
    -

    Agenda

    -

    We are going to be working off of a slide deck and tutorial written by Dean Attali. This should take two classes.

    diff --git a/content/notes/notes-b05.Rmd b/content/notes/notes-b05.Rmd index 8793175c..00dca610 100644 --- a/content/notes/notes-b05.Rmd +++ b/content/notes/notes-b05.Rmd @@ -23,14 +23,6 @@ Written material: - [R4DS Chapter 16: regular expressions](https://r4ds.hadley.nz/regexps). - The stat545.com Chapter 11 on character vectors [has an elaborate discussion on useful resources](https://stat545.com/character-vectors.html#resources-3) for learning more about strings. -## Test Your Understanding - -Use these questions to check your understanding of the material. - -1. True or False: The regular expression `[ab][ab]` will match "ab", "aa", and "bb" as possibilities, whereas `[ab]{2}` will only match "aa" or "bb". -2. True or False: The regular expression `[ab][ab]` will match "ab", "aa", and "bb" as possibilities, whereas `(ab)(ab)` will only match "aa" or "bb". -3. True or False: The regular expression `^ab` will match "ab" as the first characters to a string, whereas `[^ab]` will match "a" or "b" as being the first character to a string. - ## Strings You've used a bunch of strings at this point without knowing explicitly what they are: any time you surround text by `"`, you've been making a *string*: a storage format for text. In R, they are of type "character". @@ -58,6 +50,14 @@ You can see more examples of special characters and how to escape them in [R4DS Our main tools for working with strings will be the powerful `stringr` package in the tidyverse paired with regular expressions. We think the best way to start learning these is through the guided tutorial in Worksheet B2. +## Test Your Understanding + +Use these questions to check your understanding of the material. + +1. True or False: The regular expression `[ab][ab]` will match "ab", "aa", and "bb" as possibilities, whereas `[ab]{2}` will only match "aa" or "bb". +2. True or False: The regular expression `[ab][ab]` will match "ab", "aa", and "bb" as possibilities, whereas `(ab)(ab)` will only match "aa" or "bb". +3. True or False: The regular expression `^ab` will match "ab" as the first characters to a string, whereas `[^ab]` will match "a" or "b" as being the first character to a string. + ### Agenda ## Class 1 diff --git a/content/notes/notes-b05.html b/content/notes/notes-b05.html index 70996fd5..9a21b838 100644 --- a/content/notes/notes-b05.html +++ b/content/notes/notes-b05.html @@ -28,15 +28,6 @@

    Resources

  • The stat545.com Chapter 11 on character vectors has an elaborate discussion on useful resources for learning more about strings.
  • -
    -

    Test Your Understanding

    -

    Use these questions to check your understanding of the material.

    -
      -
    1. True or False: The regular expression [ab][ab] will match “ab”, “aa”, and “bb” as possibilities, whereas [ab]{2} will only match “aa” or “bb”.
    2. -
    3. True or False: The regular expression [ab][ab] will match “ab”, “aa”, and “bb” as possibilities, whereas (ab)(ab) will only match “aa” or “bb”.
    4. -
    5. True or False: The regular expression ^ab will match “ab” as the first characters to a string, whereas [^ab] will match “a” or “b” as being the first character to a string.
    6. -
    -

    Strings

    You’ve used a bunch of strings at this point without knowing explicitly what they are: any time you surround text by ", you’ve been making a string: a storage format for text. In R, they are of type “character”.

    @@ -57,6 +48,15 @@

    Strings

    Working with strings

    Our main tools for working with strings will be the powerful stringr package in the tidyverse paired with regular expressions. We think the best way to start learning these is through the guided tutorial in Worksheet B2.

    +
    +
    +

    Test Your Understanding

    +

    Use these questions to check your understanding of the material.

    +
      +
    1. True or False: The regular expression [ab][ab] will match “ab”, “aa”, and “bb” as possibilities, whereas [ab]{2} will only match “aa” or “bb”.
    2. +
    3. True or False: The regular expression [ab][ab] will match “ab”, “aa”, and “bb” as possibilities, whereas (ab)(ab) will only match “aa” or “bb”.
    4. +
    5. True or False: The regular expression ^ab will match “ab” as the first characters to a string, whereas [^ab] will match “a” or “b” as being the first character to a string.
    6. +

    Agenda

    diff --git a/content/notes/notes-b06.Rmd b/content/notes/notes-b06.Rmd index 7514d7bf..70bc1a1e 100644 --- a/content/notes/notes-b06.Rmd +++ b/content/notes/notes-b06.Rmd @@ -40,15 +40,6 @@ Want to dig deeper? These resources can help. - [Advanced R Chapter 9: Functionals](https://adv-r.hadley.nz/functionals.html) -- looking at purrr and `map()` from a programming perspective. - [tidyr's rectangling vignette](https://tidyr.tidyverse.org/articles/rectangle.html) -- for handling deeply nested lists (JSON-style data), similar to tidyr's `pivot_` functions. -## Test Your Understanding - -1. True or False: `map(1:3, ~ function(x) x ^ 2)` returns the list `list(1, 4, 9)`. -2. You write a function `square()` that squares its input -- but the first thing it does is print a message to the screen! True or False: `map_dbl(1:10, square)` will throw an error, because the output is not a `dbl` (a number) -- it contains the message, too. -3. True or False: purrr-style functions, starting with `~`, can be used in dplyr's `across()` function, such as `mutate(across(where(is.numeric), ~ .x ^ 2))`, and this function can only take one argument (`.x`) -4. True or False: If I have 10 tibbles I want to save to file, and they're all stored in a list, the best purrr function to use for saving these to file is `map()`. -5. True or False: Just as `c(c(1, 2), c(3, 4))` returns the vector of numbers from 1 to 4, `list(list(1, 2), list(3, 4))` returns the list of numbers from 1 to 4. -6. True or False: `tibble(model = lm(mpg ~ wt, data = mtcars))` doesn't work because it doesn't use a `map` function. - ## Vectors vs Lists Here is a list in R; it holds multiple items. @@ -130,6 +121,17 @@ head(got_lc) Some characters have one title (e.g. Will); others have more than one title (e.g. Theon Greyjoy). Consequently, the `titles` column is a *list column*, where each entry is a *list* that contains as many or as few strings as we like. +## Test Your Understanding + +1. True or False: `map(1:3, ~ function(x) x ^ 2)` returns the list `list(1, 4, 9)`. +2. You write a function `square()` that squares its input -- but the first thing it does is print a message to the screen! True or False: `map_dbl(1:10, square)` will throw an error, because the output is not a `dbl` (a number) -- it contains the message, too. +3. True or False: purrr-style functions, starting with `~`, can be used in dplyr's `across()` function, such as `mutate(across(where(is.numeric), ~ .x ^ 2))`, and this function can only take one argument (`.x`) +4. True or False: If I have 10 tibbles I want to save to file, and they're all stored in a list, the best purrr function to use for saving these to file is `map()`. +5. True or False: Just as `c(c(1, 2), c(3, 4))` returns the vector of numbers from 1 to 4, `list(list(1, 2), list(3, 4))` returns the list of numbers from 1 to 4. +6. True or False: `tibble(model = lm(mpg ~ wt, data = mtcars))` doesn't work because it doesn't use a `map` function. + +___ + ## Your turn: Worksheet B3, Parts 2 and 3 We think the best way to learn how to make and work with list columns (and get a taste for where they can be really useful!) is to jump back into the worksheet. diff --git a/content/notes/notes-b06.html b/content/notes/notes-b06.html index ed440c57..366beae3 100644 --- a/content/notes/notes-b06.html +++ b/content/notes/notes-b06.html @@ -40,17 +40,6 @@

    Resources

  • tidyr’s rectangling vignette – for handling deeply nested lists (JSON-style data), similar to tidyr’s pivot_ functions.
  • -
    -

    Test Your Understanding

    -
      -
    1. True or False: map(1:3, ~ function(x) x ^ 2) returns the list list(1, 4, 9).
    2. -
    3. You write a function square() that squares its input – but the first thing it does is print a message to the screen! True or False: map_dbl(1:10, square) will throw an error, because the output is not a dbl (a number) – it contains the message, too.
    4. -
    5. True or False: purrr-style functions, starting with ~, can be used in dplyr’s across() function, such as mutate(across(where(is.numeric), ~ .x ^ 2)), and this function can only take one argument (.x)
    6. -
    7. True or False: If I have 10 tibbles I want to save to file, and they’re all stored in a list, the best purrr function to use for saving these to file is map().
    8. -
    9. True or False: Just as c(c(1, 2), c(3, 4)) returns the vector of numbers from 1 to 4, list(list(1, 2), list(3, 4)) returns the list of numbers from 1 to 4.
    10. -
    11. True or False: tibble(model = lm(mpg ~ wt, data = mtcars)) doesn’t work because it doesn’t use a map function.
    12. -
    -

    Vectors vs Lists

    Here is a list in R; it holds multiple items.

    @@ -68,21 +57,21 @@

    Vectors vs Lists

    - + - + - + - + @@ -142,6 +131,18 @@

    List Columns

    ## 6 Chett Male <chr [1]>

    Some characters have one title (e.g. Will); others have more than one title (e.g. Theon Greyjoy). Consequently, the titles column is a list column, where each entry is a list that contains as many or as few strings as we like.

    +
    +

    Test Your Understanding

    +
      +
    1. True or False: map(1:3, ~ function(x) x ^ 2) returns the list list(1, 4, 9).
    2. +
    3. You write a function square() that squares its input – but the first thing it does is print a message to the screen! True or False: map_dbl(1:10, square) will throw an error, because the output is not a dbl (a number) – it contains the message, too.
    4. +
    5. True or False: purrr-style functions, starting with ~, can be used in dplyr’s across() function, such as mutate(across(where(is.numeric), ~ .x ^ 2)), and this function can only take one argument (.x)
    6. +
    7. True or False: If I have 10 tibbles I want to save to file, and they’re all stored in a list, the best purrr function to use for saving these to file is map().
    8. +
    9. True or False: Just as c(c(1, 2), c(3, 4)) returns the vector of numbers from 1 to 4, list(list(1, 2), list(3, 4)) returns the list of numbers from 1 to 4.
    10. +
    11. True or False: tibble(model = lm(mpg ~ wt, data = mtcars)) doesn’t work because it doesn’t use a map function.
    12. +
    +
    +

    Your turn: Worksheet B3, Parts 2 and 3

    We think the best way to learn how to make and work with list columns (and get a taste for where they can be really useful!) is to jump back into the worksheet.

    diff --git a/static/notes/notes-a06_files/figure-html/unnamed-chunk-10-1.png b/static/notes/notes-a06_files/figure-html/unnamed-chunk-10-1.png index ed3ad9b9..a0e9c6b3 100644 Binary files a/static/notes/notes-a06_files/figure-html/unnamed-chunk-10-1.png and b/static/notes/notes-a06_files/figure-html/unnamed-chunk-10-1.png differ diff --git a/static/notes/notes-a06_files/figure-html/unnamed-chunk-12-1.png b/static/notes/notes-a06_files/figure-html/unnamed-chunk-12-1.png index 0e7a494b..36a75c56 100644 Binary files a/static/notes/notes-a06_files/figure-html/unnamed-chunk-12-1.png and b/static/notes/notes-a06_files/figure-html/unnamed-chunk-12-1.png differ diff --git a/static/notes/notes-a06_files/figure-html/unnamed-chunk-14-1.png b/static/notes/notes-a06_files/figure-html/unnamed-chunk-14-1.png index 4f80e1d6..86431e1f 100644 Binary files a/static/notes/notes-a06_files/figure-html/unnamed-chunk-14-1.png and b/static/notes/notes-a06_files/figure-html/unnamed-chunk-14-1.png differ diff --git a/static/notes/notes-a06_files/figure-html/unnamed-chunk-3-1.png b/static/notes/notes-a06_files/figure-html/unnamed-chunk-3-1.png index 353d603b..164b0d5d 100644 Binary files a/static/notes/notes-a06_files/figure-html/unnamed-chunk-3-1.png and b/static/notes/notes-a06_files/figure-html/unnamed-chunk-3-1.png differ diff --git a/static/notes/notes-a06_files/figure-html/unnamed-chunk-5-1.png b/static/notes/notes-a06_files/figure-html/unnamed-chunk-5-1.png index 6a9a0131..ced712c0 100644 Binary files a/static/notes/notes-a06_files/figure-html/unnamed-chunk-5-1.png and b/static/notes/notes-a06_files/figure-html/unnamed-chunk-5-1.png differ diff --git a/static/notes/notes-a06_files/figure-html/unnamed-chunk-6-1.png b/static/notes/notes-a06_files/figure-html/unnamed-chunk-6-1.png index 27b668c4..e294bd66 100644 Binary files a/static/notes/notes-a06_files/figure-html/unnamed-chunk-6-1.png and b/static/notes/notes-a06_files/figure-html/unnamed-chunk-6-1.png differ diff --git a/static/notes/notes-a06_files/figure-html/unnamed-chunk-7-1.png b/static/notes/notes-a06_files/figure-html/unnamed-chunk-7-1.png index 353d603b..164b0d5d 100644 Binary files a/static/notes/notes-a06_files/figure-html/unnamed-chunk-7-1.png and b/static/notes/notes-a06_files/figure-html/unnamed-chunk-7-1.png differ diff --git a/static/notes/notes-a06_files/figure-html/unnamed-chunk-9-1.png b/static/notes/notes-a06_files/figure-html/unnamed-chunk-9-1.png index 8b219edb..c3aeb5b9 100644 Binary files a/static/notes/notes-a06_files/figure-html/unnamed-chunk-9-1.png and b/static/notes/notes-a06_files/figure-html/unnamed-chunk-9-1.png differ diff --git a/static/notes/notes-a09_files/figure-html/unnamed-chunk-21-1.png b/static/notes/notes-a09_files/figure-html/unnamed-chunk-21-1.png index f84d3a1f..cbec5977 100644 Binary files a/static/notes/notes-a09_files/figure-html/unnamed-chunk-21-1.png and b/static/notes/notes-a09_files/figure-html/unnamed-chunk-21-1.png differ
    Grammar Component Specification
    data gapminder
    aesthetic mapping x: gdpPercap, y: lifeExp
    geometric object points
    scale x: log10, y: linear
    statistical transform none
    coordinate system rectangular
    facetting none
    Vectors Lists
    Access elements with square brackets [] Access elements with [[]]
    Each element must be an atomic data type (i.e. a single value) Elements can be anything, even another list or another vector
    Each element has to be of the same type Elements can be as different as you like