From e22364e71cb07c8eb9e4499163550dd561b7c772 Mon Sep 17 00:00:00 2001 From: njlyon0 Date: Thu, 6 Jun 2024 19:45:25 -0400 Subject: [PATCH] Fixed typo in conditional bit --- _freeze/materials/slides_2a/execute-results/html.json | 8 +++++--- materials/slides_2a.qmd | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/_freeze/materials/slides_2a/execute-results/html.json b/_freeze/materials/slides_2a/execute-results/html.json index 6f018a1..ebe2de2 100644 --- a/_freeze/materials/slides_2a/execute-results/html.json +++ b/_freeze/materials/slides_2a/execute-results/html.json @@ -1,9 +1,11 @@ { - "hash": "2a0a926f9b6f5866995aeed18dc8eb19", + "hash": "ca45565f91ad42236bcf611b2dd320e9", "result": { "engine": "knitr", - "markdown": "---\ntitle: \"Intro to Data Science\"\nauthor: \"Lecture 2 -- Packages, Structure, & Conditionals\"\nengine: knitr\nformat: \n revealjs: \n slide-number: c\n scrollable: false\n code-overflow: wrap\n code-line-numbers: false\n code-copy: hover\n theme: [night, slides.scss]\n reference-location: document\n footer: \"[Programming in R for Biologists](https://njlyon0.github.io/teach_r-for-biologists/)\"\n---\n\n\n## A Guide to Your Process\n\n### [Scheduling]{.blue}\n\n### [Learning Objectives]{.purple}\n\n### [Practice]{.pink}\n\n### [Supporting Information]{.orange}\n\n### [Class Discussion]{.gold}\n\n## [Today's Plan]{.blue}\n\n- R Packages\n- Class vs. Structure\n- Using Vectors\n- Conditionals\n\n## [Today's Learning Objectives]{.purple}\n\nAfter today's session you will be able to:\n\n. . .\n\n- Load and use an R package\n- Define the difference(s) between object class and structure\n- Create and manipulate vectors\n- Write conditional statements\n- Manage missing data in objects with conditionals\n\n## [R Package Background]{.orange} {.smaller}\n\n- R [packages]{.purple} are groups of functions developed by users\n\n\\\n\n. . .\n\n- Packages have no defined depth or breadth requirements\n - A package could be a single, simple function\n - Or a complex ecosystem of inter-related functions\n\n\\\n\n. . .\n\n- Packages can be installed by any R user for _free!_\n\n\\\n\n. . .\n\n- R is versatile and powerful (in part) because of contributed packages\n\n## [Package Locations]{.orange}\n\n- There are two main homes for R packages\n\n\\\n\n. . . \n\n1. Comprehensive R Archive Network\n - A.k.a. \"CRAN\" | [cran.rstudio.com](https://cran.rstudio.com/)\n\n\\\n\n. . .\n\n2. GitHub\n - [github.com](https://github.com/)\n\n## [CRAN vs. GitHub]{.orange} {.smaller}\n\n::::{.columns}\n:::{.column width=\"50%\"}\n\n#### CRAN\n\n- Currently >20,000 packages live here\n\n\\\n\n- Strict rules for packages to be allowed\n\n\\\n\n- These are \"official\" packages\n\n:::\n:::{.column width=\"50%\"}\n\n#### GitHub\n\n:::\n::::\n\n## [CRAN vs. GitHub]{.orange} {.smaller}\n\n::::{.columns}\n:::{.column width=\"50%\"}\n\n#### CRAN\n\n- Currently >20,000 packages live here\n\n\\\n\n- Strict rules for packages to be allowed\n\n\\\n\n- These are \"official\" packages\n\n:::\n:::{.column width=\"50%\"}\n\n#### GitHub\n\n- Unknown number of packages here (no centralized record retained)\n\n\\\n\n- No mandatory quality control tests to be available here\n\n\\\n\n- Packages _usually_ work but they don't have the same quality control as CRAN packages\n\n:::\n::::\n\n## [Using Packages]{.orange} {.smaller}\n\n- In order to use a package, you must:\n\n\\\n\n. . .\n\n1. Install the desired package\n - Done once per computer\n\n\\\n\n. . .\n\n2. Load the package into R\n - Done _every_ time you re-open RStudio\n\n## [Using Packages: Specific Steps]{.orange} {.smaller}\n\n\\\n\n```{.r}\n# Install desired package\ninstall.packages(\"dplyr\")\n\n# Load that package\nlibrary(dplyr)\n```\n\n\\\n\n- `install.packages` _requires_ the package name be in quotes\n\n\\\n\n. . .\n\n- `library` _requires_ requires **unquoted** names.\n\n## [Package Analogy]{.gold} {.smaller}\n\n. . . \n\n- `install.packages` = buying a set of tools from the store and putting them in your home\n\n\\\n\n. . .\n\n- `library` = moving the tools that you already own to your workbench\n\n\\\n\n. . .\n\n- You only _buy_ the tools **once** but _every time_ you start work you need to bring them back to your work area!\n\n## [Practice: Packages]{.pink} {.smaller}\n\n![](images/hex-penguins.png){.absolute top=0 left=1100 width=\"12%\" fig-alt=\"palmerpenguins R package hex logo\"}\n\n- Make a new script for this week's lecture!\n - Save it in your RStudio Project folder for this course\n - Make sure it has \"lecture\" and \"2\" in the file name\n\n\\\n\n. . .\n\n- Install the `palmerpenguins` package\n - Remember to put quotes around the package name!\n\n\\\n\n. . .\n\n- Load the package with the `library` function\n - Once loaded, run `?palmerpenguins` to see the package-level help file\n\n## [Note on _Function_ Names]{.orange} {.smaller}\n\n- Functions _are not_ required to have unique names across all packages\n\n\\\n\n. . .\n\n- Risk of using a different function than intended\n - Best case: creates an error and forces you to catch the mistake\n - Worst case: _silently_ does something wrong\n\n## [Function Namespacing]{.orange} {.smaller}\n\n- Functions can be \"[namespaced]{.purple}\" to specify which package the function comes from\n\n\\\n\n. . .\n\n- Namespacing _guarantees_ you use the function from the desired package\n\n\\\n\n. . .\n\n- Done with two colons (:) between the package and function names\n - E.g., `package::function()`\n\n## [Practice: Namespacing]{.pink} {.smaller}\n\n![](images/hex-penguins.png){.absolute top=0 left=1100 width=\"12%\" fig-alt=\"palmerpenguins R package hex logo\"}\n![](images/hex-dplyr.png){.absolute top=0 left=965 width=\"12%\" fig-alt=\"dplyr R package hex logo\"}\n\n- `palmerpenguins` includes an example dataset on penguins\n\n. . .\n\n- Run the following code\n - `peng_df <- palmerpenguins::penguins`\n\n\\\n\n. . .\n\n- Install and load the `dplyr` package\n - `dplyr` has a function called `glimpse` that shows you core structures of data\n\n\\\n\n. . .\n\n- Namespace `glimpse` and run it on `peng_df`\n\n## [Temperature Check]{.purple}\n\n#### How are you Feeling?\n\n

\n\"Comic-style\n

\n\n## [Class versus Structure]{.orange} {.smaller}\n\n- Objects have both [class]{.purple} and [structure]{.purple}\n\n\\\n\n. . .\n\n- [Class]{.purple} = the type of object it is\n - E.g., dataframe, integer, character, etc.\n\n\\\n\n. . .\n\n- [Structure]{.purple} = the dimensions and \"shape\" of the data\n - E.g., Number of rows / columns, length, etc.\n\n\\\n\n. . .\n\n- _Both_ class and structure affect what you can do with or to a given object!\n\n## [Checking Class/Structure]{.orange} {.smaller}\n\n- Best to **check class and structure** of an object to ensure functions will work\n\n. . .\n\n::::{.columns}\n:::{.column width=\"50%\"}\n\n### Check Class\n\n:::\n:::{.column width=\"50%\"}\n\n### Check Structure\n\n:::\n::::\n\n## [Checking Class/Structure]{.orange} {.smaller}\n\n- Best to **check class and structure** of an object to ensure functions will work\n\n::::{.columns}\n:::{.column width=\"50%\"}\n\n### Check Class\n\n- Use the `class` function\n\n\\\n\n```{.r}\n# Checking class of 'my_obj'\nclass(my_obj)\n```\n\n:::\n:::{.column width=\"50%\"}\n\n### Check Structure\n\n:::\n::::\n\n## [Checking Class/Structure]{.orange} {.smaller}\n\n- Best to **check class and structure** of an object to ensure functions will work\n\n::::{.columns}\n:::{.column width=\"50%\"}\n\n### Check Class\n\n- Use the `class` function\n\n\\\n\n```{.r}\n# Checking class of 'my_obj'\nclass(my_obj)\n```\n\n:::\n:::{.column width=\"50%\"}\n\n### Check Structure\n\n- Use _either_ the `str` function _or_ `dplyr::glimpse`\n\n```{.r}\n# Checking structure of 'my_obj' (with base R)\nstr(my_obj)\n\n# Checking structure of 'my_obj' (with `dplyr`)\ndplyr::glimpse(my_obj)\n```\n\n:::\n::::\n\n## [Practice: Structure]{.pink} {.smaller}\n\n- What is the [class]{.orange} of 'peng_df'?\n\n\\\n\n. . .\n\n- What is the [structure]{.orange}?\n - What information is included when you check?\n\n\\\n\n. . .\n\n- What happens when you check the class of a function?\n - Run `class(class)`\n\n\\\n\n. . .\n\n- What happens when you check the structure of a function?\n - Run `str(str)`\n\n## [Using Vectors: Coordinates]{.orange} {.smaller}\n\n- Vector structure is expressed as \"[length]{.purple}\"\n - Vector length = number of elements in the vector\n - Dataframe length = number of rows\n\n\\\n\n. . .\n\n- [Bracket notation]{.orange} can be used to navigate vectors\n\n\\\n\n. . .\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Make a vector\nmy_vec <- c(\"a\", \"b\", \"c\", \"d\", \"e\")\n\n# Use bracket notation to retrieve one element\nmy_vec[3]\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] \"c\"\n```\n\n\n:::\n:::\n\n\n## [Using Vectors: Coordinates Cont.]{.orange} {.smaller}\n\n- Bracket notation accepts vectors of coordinates\n\n\\\n\n. . .\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Use bracket notation to retrieve several elements\nmy_vec[c(1, 3, 5)]\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] \"a\" \"c\" \"e\"\n```\n\n\n:::\n:::\n\n\n\\\n\n. . .\n\n- You can also grab the same element _more than once!_\n\n\\\n\n. . .\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Use bracket notation to retrieve one element multiple times\nmy_vec[c(1, 1, 1)]\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] \"a\" \"a\" \"a\"\n```\n\n\n:::\n:::\n\n\n## [Practice: Vector Navigation]{.pink} {.smaller}\n\n- Base R has a built-in vector of letters called `letters`\n - Assign `letters` to an object called `my_vec`\n\n\\\n\n. . .\n\n- Check the length of `my_vec` using the `length` function\n\n\\\n\n. . .\n \n- Identify the 10^th^ element of `my_vec` using bracket notation\n\n\\\n\n- Identify the 8^th^, 5^th^, 12^th^, 12^th^ (again), and 15^th^ elements of `my_vec`\n - Use concatenation inside of the brackets (with `c`)!\n\n## [Temperature Check]{.purple}\n\n#### How are you Feeling?\n\n

\n\"Comic-style\n

\n\n## [Conditionals]{.orange} {.smaller}\n\n- You can write code that runs _only_ if an 'if statement' is true\n - Otherwise that chunk of code is skipped!\n\n\\\n\n. . .\n\n- This allows you to write flexible code that can handle any outcome that you can anticipate!\n - Particularly useful for subsetting data based on the contents of a column\n\n\\\n\n. . .\n\n- These 'if statements' are called [conditionals]{.purple}\n\n\\\n\n. . .\n\n- The answer to a conditional _must_ be either `TRUE` or `FALSE`\n\n## [Fundamentals: EQUAL]{.orange} {.smaller}\n\n- Are two things exactly equal?\n\n\\\n\n. . .\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Check a conditional\n\"hello\" == \"hello\"\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] TRUE\n```\n\n\n:::\n:::\n\n\n\\\n\n. . .\n\n- Uses `==` operator\n - Just two equal signs\n\n## [Fundamentals: OR]{.orange} {.smaller}\n\n- Are _any_ of these conditions met?\n\n\\\n\n. . .\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Check either one conditional *or* the other\n\"hello\" == \"hello\" | 2 == 7\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] TRUE\n```\n\n\n:::\n:::\n\n\n\\\n\n. . .\n\n- Uses `|` operator\n - Shift + `\\` on keyboard\n\n\n## [Fundamentals: AND]{.orange} {.smaller}\n\n- Are _all_ of the conditions met?\n\n\\\n\n. . .\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Are *all* conditions TRUE?\n\"hello\" == \"hello\" & 2 == 7\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] FALSE\n```\n\n\n:::\n:::\n\n\n\\\n\n. . .\n\n- Uses `&` operator\n - Shift + 7 on keyboard\n\n## [Fundamentals: Summary]{.orange} {.smaller}\n\n::::{.columns}\n:::{.column width=\"33.33%\"}\n### EQUAL\n\n- Are two things exactly equal?\n\n\\\n\n\n::: {.cell}\n\n```{.r .cell-code}\n\"hello\" == \"hello\"\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] TRUE\n```\n\n\n:::\n:::\n\n\n\\\n\n- Uses `==` operator\n - Just two equal signs\n\n:::\n:::{.column width=\"33.33%\"}\n\n### OR\n\n- Are _any_ of these conditions met?\n\n\\\n\n\n::: {.cell}\n\n```{.r .cell-code}\n\"hello\" == \"hello\" | 2 == 7\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] TRUE\n```\n\n\n:::\n:::\n\n\n\\\n\n- Uses `|` operator\n - Shift + `\\` on keyboard\n\n:::\n:::{.column width=\"33.33%\"}\n\n### AND\n\n- Are _all_ of the conditions met?\n\n\\\n\n\n::: {.cell}\n\n```{.r .cell-code}\n\"hello\" == \"hello\" & 2 == 7\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] FALSE\n```\n\n\n:::\n:::\n\n\n\\\n\n- Uses `&` operator\n - Shift + 7 on keyboard\n\n:::\n::::\n\n## [Practice: Fundamental Conditionals]{.pink} {.smaller}\n\n![](images/hex-penguins.png){.absolute top=0 left=1100 width=\"12%\" fig-alt=\"palmerpenguins R package hex logo\"}\n\n- We'll use the base R `subset` function with the `peng_df` object\n - If needed, consult the help file for more details (`?subset`)\n\n\\\n\n. . .\n\n- Subset `peng_df` to only Adelie penguins (and assign to a `sub_v1` object)\n - I.e., `species == \"Adelie\"`\n\n\\\n\n. . .\n\n- How many rows does that subset have?\n\n## [More Practice: Fundamental Conditionals]{.pink} {.smaller}\n\n![](images/hex-penguins.png){.absolute top=0 left=1100 width=\"12%\" fig-alt=\"palmerpenguins R package hex logo\"}\n\n- Subset `peng_df` to Adelie _or_ Biscoe penguins\n - Assign this subset to `sub_v2` object\n\n\\\n\n. . .\n\n- Subset `peng_df` to only male Gentoo penguins\n - Assign to `sub_v3` object\n\n\\\n\n. . .\n\n- How many rows does _that_ subset have?\n\n## [Discussion: Conditionals]{.gold} {.smaller}\n\n- We've covered EQUAL, OR, and AND\n - `==`, `|`, or `&`\n\n\\\n\n. . .\n\n- What unanswered questions do you have?\n\n\\\n\n. . .\n\n- What other types of conditional statements would be useful?\n - Think about it in the context of wanting to subset some data\n\n## [Numeric Conditionals]{.orange} {.smaller}\n\n- For numbers, we can use greater/less than conditionals!\n\n\\\n\n. . .\n\n- Greater / less than are expressed as normal\n - `>` and `<`\n\n\\\n\n. . .\n\n- Adding 'or equal to' is done by adding an equal sign\n - `>=` and `<=`\n\n## [Practice: Numeric Conditionals]{.pink} {.smaller}\n\n![](images/hex-penguins.png){.absolute top=0 left=1100 width=\"12%\" fig-alt=\"palmerpenguins R package hex logo\"}\n\n- Subset `peng_df` to only penguins with a bill length greater than 40 mm\n - Assign to `sub_v7`\n\n\\\n\n. . . \n\n- Subset `peng_df` to only penguins with a body mass less than or equal to 4,000 g\n - Assign to `sub_v8`\n\n## [Temperature Check]{.purple}\n\n#### How are you Feeling?\n\n

\n\"Comic-style\n

\n\n## [Upcoming Due Dates]{.blue} {.smaller}\n\n::::{.columns}\n\n:::{.column width=\"50%\"}\n\n### Due before lab\n\n#### (By midnight)\n\n- Muddiest Point \\#2\n- Read [Excuse me, do you have a moment to talk about version control?](https://peerj.com/preprints/3159/)\n\n:::\n\n:::{.column width=\"50%\"}\n\n### Due before lecture\n\n#### (By midnight)\n\n- Homework \\#2\n- Read the British Ecological Society's [Reproducible Code Guide](https://www.britishecologicalsociety.org/wp-content/uploads/2017/12/guide-to-reproducible-code.pdf) (p.20-25)\n\n:::\n\n::::\n\n# [Bonus Conditionals]{.orange}\n\n## [OR with >2 Options]{.orange} {.smaller}\n\n- OR conditionals with many options get cumbersome quickly\n - E.g., `x == 1 | x == 2 | x == 3 | x == 4 …`\n\n\\\n\n. . .\n\n- We can use concatenation and the `%in%` operator to simplify this!\n\n\\\n\n. . .\n\n- `%in%` is effectively \"if any of \\ matches the value\"\n - E.g., `x %in% c(1, 2, 3, 4, …)`\n\n## [Conditionals: NOT]{.orange} {.smaller}\n\n- You can also _exclude_ based on conditions\n - Two different ways of doing this\n\n\\\n\n. . . \n\n- For one / a few options: use `!=` for \"not equal to\"\n - E.g., `x != 10`\n\n\\\n\n. . . \n\n- Can be combined with `%in%` to exclude a set of options\n - E.g., `!x %in% c(...)`\n - Note the exclamation mark is _before_ the object\n\n## [Practice: Advanced Conditionals]{.pink} {.smaller}\n\n![](images/hex-penguins.png){.absolute top=0 left=1100 width=\"12%\" fig-alt=\"palmerpenguins R package hex logo\"}\n\n- Subset `peng_df` is species is any of \"Adelie\", \"Gentoo\", or \"Chinstrap\"\n - Use the `%in%` operator\n\n\\\n\n. . .\n\n- Subset `peng_df` to all islands except \"Dream\"\n", - "supporting": [], + "markdown": "---\ntitle: \"Intro to Data Science\"\nauthor: \"Lecture 2 -- Packages, Structure, & Conditionals\"\nengine: knitr\nformat: \n revealjs: \n slide-number: c\n scrollable: false\n code-overflow: wrap\n code-line-numbers: false\n code-copy: hover\n theme: [night, slides.scss]\n reference-location: document\n footer: \"[Programming in R for Biologists](https://njlyon0.github.io/teach_r-for-biologists/)\"\n---\n\n\n## A Guide to Your Process\n\n### [Scheduling]{.blue}\n\n### [Learning Objectives]{.purple}\n\n### [Practice]{.pink}\n\n### [Supporting Information]{.orange}\n\n### [Class Discussion]{.gold}\n\n## [Today's Plan]{.blue}\n\n- R Packages\n- Class vs. Structure\n- Using Vectors\n- Conditionals\n\n## [Today's Learning Objectives]{.purple}\n\nAfter today's session you will be able to:\n\n. . .\n\n- Load and use an R package\n- Define the difference(s) between object class and structure\n- Create and manipulate vectors\n- Write conditional statements\n- Manage missing data in objects with conditionals\n\n## [R Package Background]{.orange} {.smaller}\n\n- R [packages]{.purple} are groups of functions developed by users\n\n\\\n\n. . .\n\n- Packages have no defined depth or breadth requirements\n - A package could be a single, simple function\n - Or a complex ecosystem of inter-related functions\n\n\\\n\n. . .\n\n- Packages can be installed by any R user for _free!_\n\n\\\n\n. . .\n\n- R is versatile and powerful (in part) because of contributed packages\n\n## [Package Locations]{.orange}\n\n- There are two main homes for R packages\n\n\\\n\n. . . \n\n1. Comprehensive R Archive Network\n - A.k.a. \"CRAN\" | [cran.rstudio.com](https://cran.rstudio.com/)\n\n\\\n\n. . .\n\n2. GitHub\n - [github.com](https://github.com/)\n\n## [CRAN vs. GitHub]{.orange} {.smaller}\n\n::::{.columns}\n:::{.column width=\"50%\"}\n\n#### CRAN\n\n- Currently >20,000 packages live here\n\n\\\n\n- Strict rules for packages to be allowed\n\n\\\n\n- These are \"official\" packages\n\n:::\n:::{.column width=\"50%\"}\n\n#### GitHub\n\n:::\n::::\n\n## [CRAN vs. GitHub]{.orange} {.smaller}\n\n::::{.columns}\n:::{.column width=\"50%\"}\n\n#### CRAN\n\n- Currently >20,000 packages live here\n\n\\\n\n- Strict rules for packages to be allowed\n\n\\\n\n- These are \"official\" packages\n\n:::\n:::{.column width=\"50%\"}\n\n#### GitHub\n\n- Unknown number of packages here (no centralized record retained)\n\n\\\n\n- No mandatory quality control tests to be available here\n\n\\\n\n- Packages _usually_ work but they don't have the same quality control as CRAN packages\n\n:::\n::::\n\n## [Using Packages]{.orange} {.smaller}\n\n- In order to use a package, you must:\n\n\\\n\n. . .\n\n1. Install the desired package\n - Done once per computer\n\n\\\n\n. . .\n\n2. Load the package into R\n - Done _every_ time you re-open RStudio\n\n## [Using Packages: Specific Steps]{.orange} {.smaller}\n\n\\\n\n```{.r}\n# Install desired package\ninstall.packages(\"dplyr\")\n\n# Load that package\nlibrary(dplyr)\n```\n\n\\\n\n- `install.packages` _requires_ the package name be in quotes\n\n\\\n\n. . .\n\n- `library` _requires_ requires **unquoted** names.\n\n## [Package Analogy]{.gold} {.smaller}\n\n. . . \n\n- `install.packages` = buying a set of tools from the store and putting them in your home\n\n\\\n\n. . .\n\n- `library` = moving the tools that you already own to your workbench\n\n\\\n\n. . .\n\n- You only _buy_ the tools **once** but _every time_ you start work you need to bring them back to your work area!\n\n## [Practice: Packages]{.pink} {.smaller}\n\n![](images/hex-penguins.png){.absolute top=0 left=1100 width=\"12%\" fig-alt=\"palmerpenguins R package hex logo\"}\n\n- Make a new script for this week's lecture!\n - Save it in your RStudio Project folder for this course\n - Make sure it has \"lecture\" and \"2\" in the file name\n\n\\\n\n. . .\n\n- Install the `palmerpenguins` package\n - Remember to put quotes around the package name!\n\n\\\n\n. . .\n\n- Load the package with the `library` function\n - Once loaded, run `?palmerpenguins` to see the package-level help file\n\n## [Note on _Function_ Names]{.orange} {.smaller}\n\n- Functions _are not_ required to have unique names across all packages\n\n\\\n\n. . .\n\n- Risk of using a different function than intended\n - Best case: creates an error and forces you to catch the mistake\n - Worst case: _silently_ does something wrong\n\n## [Function Namespacing]{.orange} {.smaller}\n\n- Functions can be \"[namespaced]{.purple}\" to specify which package the function comes from\n\n\\\n\n. . .\n\n- Namespacing _guarantees_ you use the function from the desired package\n\n\\\n\n. . .\n\n- Done with two colons (:) between the package and function names\n - E.g., `package::function()`\n\n## [Practice: Namespacing]{.pink} {.smaller}\n\n![](images/hex-penguins.png){.absolute top=0 left=1100 width=\"12%\" fig-alt=\"palmerpenguins R package hex logo\"}\n![](images/hex-dplyr.png){.absolute top=0 left=965 width=\"12%\" fig-alt=\"dplyr R package hex logo\"}\n\n- `palmerpenguins` includes an example dataset on penguins\n\n. . .\n\n- Run the following code\n - `peng_df <- palmerpenguins::penguins`\n\n\\\n\n. . .\n\n- Install and load the `dplyr` package\n - `dplyr` has a function called `glimpse` that shows you core structures of data\n\n\\\n\n. . .\n\n- Namespace `glimpse` and run it on `peng_df`\n\n## [Temperature Check]{.purple}\n\n#### How are you Feeling?\n\n

\n\"Comic-style\n

\n\n## [Class versus Structure]{.orange} {.smaller}\n\n- Objects have both [class]{.purple} and [structure]{.purple}\n\n\\\n\n. . .\n\n- [Class]{.purple} = the type of object it is\n - E.g., dataframe, integer, character, etc.\n\n\\\n\n. . .\n\n- [Structure]{.purple} = the dimensions and \"shape\" of the data\n - E.g., Number of rows / columns, length, etc.\n\n\\\n\n. . .\n\n- _Both_ class and structure affect what you can do with or to a given object!\n\n## [Checking Class/Structure]{.orange} {.smaller}\n\n- Best to **check class and structure** of an object to ensure functions will work\n\n. . .\n\n::::{.columns}\n:::{.column width=\"50%\"}\n\n### Check Class\n\n:::\n:::{.column width=\"50%\"}\n\n### Check Structure\n\n:::\n::::\n\n## [Checking Class/Structure]{.orange} {.smaller}\n\n- Best to **check class and structure** of an object to ensure functions will work\n\n::::{.columns}\n:::{.column width=\"50%\"}\n\n### Check Class\n\n- Use the `class` function\n\n\\\n\n```{.r}\n# Checking class of 'my_obj'\nclass(my_obj)\n```\n\n:::\n:::{.column width=\"50%\"}\n\n### Check Structure\n\n:::\n::::\n\n## [Checking Class/Structure]{.orange} {.smaller}\n\n- Best to **check class and structure** of an object to ensure functions will work\n\n::::{.columns}\n:::{.column width=\"50%\"}\n\n### Check Class\n\n- Use the `class` function\n\n\\\n\n```{.r}\n# Checking class of 'my_obj'\nclass(my_obj)\n```\n\n:::\n:::{.column width=\"50%\"}\n\n### Check Structure\n\n- Use _either_ the `str` function _or_ `dplyr::glimpse`\n\n```{.r}\n# Checking structure of 'my_obj' (with base R)\nstr(my_obj)\n\n# Checking structure of 'my_obj' (with `dplyr`)\ndplyr::glimpse(my_obj)\n```\n\n:::\n::::\n\n## [Practice: Structure]{.pink} {.smaller}\n\n- What is the [class]{.orange} of 'peng_df'?\n\n\\\n\n. . .\n\n- What is the [structure]{.orange}?\n - What information is included when you check?\n\n\\\n\n. . .\n\n- What happens when you check the class of a function?\n - Run `class(class)`\n\n\\\n\n. . .\n\n- What happens when you check the structure of a function?\n - Run `str(str)`\n\n## [Using Vectors: Coordinates]{.orange} {.smaller}\n\n- Vector structure is expressed as \"[length]{.purple}\"\n - Vector length = number of elements in the vector\n - Dataframe length = number of rows\n\n\\\n\n. . .\n\n- [Bracket notation]{.orange} can be used to navigate vectors\n\n\\\n\n. . .\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Make a vector\nmy_vec <- c(\"a\", \"b\", \"c\", \"d\", \"e\")\n\n# Use bracket notation to retrieve one element\nmy_vec[3]\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] \"c\"\n```\n\n\n:::\n:::\n\n\n## [Using Vectors: Coordinates Cont.]{.orange} {.smaller}\n\n- Bracket notation accepts vectors of coordinates\n\n\\\n\n. . .\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Use bracket notation to retrieve several elements\nmy_vec[c(1, 3, 5)]\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] \"a\" \"c\" \"e\"\n```\n\n\n:::\n:::\n\n\n\\\n\n. . .\n\n- You can also grab the same element _more than once!_\n\n\\\n\n. . .\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Use bracket notation to retrieve one element multiple times\nmy_vec[c(1, 1, 1)]\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] \"a\" \"a\" \"a\"\n```\n\n\n:::\n:::\n\n\n## [Practice: Vector Navigation]{.pink} {.smaller}\n\n- Base R has a built-in vector of letters called `letters`\n - Assign `letters` to an object called `my_vec`\n\n\\\n\n. . .\n\n- Check the length of `my_vec` using the `length` function\n\n\\\n\n. . .\n \n- Identify the 10^th^ element of `my_vec` using bracket notation\n\n\\\n\n- Identify the 8^th^, 5^th^, 12^th^, 12^th^ (again), and 15^th^ elements of `my_vec`\n - Use concatenation inside of the brackets (with `c`)!\n\n## [Temperature Check]{.purple}\n\n#### How are you Feeling?\n\n

\n\"Comic-style\n

\n\n## [Conditionals]{.orange} {.smaller}\n\n- You can write code that runs _only_ if an 'if statement' is true\n - Otherwise that chunk of code is skipped!\n\n\\\n\n. . .\n\n- This allows you to write flexible code that can handle any outcome that you can anticipate!\n - Particularly useful for subsetting data based on the contents of a column\n\n\\\n\n. . .\n\n- These 'if statements' are called [conditionals]{.purple}\n\n\\\n\n. . .\n\n- The answer to a conditional _must_ be either `TRUE` or `FALSE`\n\n## [Fundamentals: EQUAL]{.orange} {.smaller}\n\n- Are two things exactly equal?\n\n\\\n\n. . .\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Check a conditional\n\"hello\" == \"hello\"\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] TRUE\n```\n\n\n:::\n:::\n\n\n\\\n\n. . .\n\n- Uses `==` operator\n - Just two equal signs\n\n## [Fundamentals: OR]{.orange} {.smaller}\n\n- Are _any_ of these conditions met?\n\n\\\n\n. . .\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Check either one conditional *or* the other\n\"hello\" == \"hello\" | 2 == 7\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] TRUE\n```\n\n\n:::\n:::\n\n\n\\\n\n. . .\n\n- Uses `|` operator\n - Shift + `\\` on keyboard\n\n\n## [Fundamentals: AND]{.orange} {.smaller}\n\n- Are _all_ of the conditions met?\n\n\\\n\n. . .\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Are *all* conditions TRUE?\n\"hello\" == \"hello\" & 2 == 7\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] FALSE\n```\n\n\n:::\n:::\n\n\n\\\n\n. . .\n\n- Uses `&` operator\n - Shift + 7 on keyboard\n\n## [Fundamentals: Summary]{.orange} {.smaller}\n\n::::{.columns}\n:::{.column width=\"33.33%\"}\n### EQUAL\n\n- Are two things exactly equal?\n\n\\\n\n\n::: {.cell}\n\n```{.r .cell-code}\n\"hello\" == \"hello\"\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] TRUE\n```\n\n\n:::\n:::\n\n\n\\\n\n- Uses `==` operator\n - Just two equal signs\n\n:::\n:::{.column width=\"33.33%\"}\n\n### OR\n\n- Are _any_ of these conditions met?\n\n\\\n\n\n::: {.cell}\n\n```{.r .cell-code}\n\"hello\" == \"hello\" | 2 == 7\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] TRUE\n```\n\n\n:::\n:::\n\n\n\\\n\n- Uses `|` operator\n - Shift + `\\` on keyboard\n\n:::\n:::{.column width=\"33.33%\"}\n\n### AND\n\n- Are _all_ of the conditions met?\n\n\\\n\n\n::: {.cell}\n\n```{.r .cell-code}\n\"hello\" == \"hello\" & 2 == 7\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] FALSE\n```\n\n\n:::\n:::\n\n\n\\\n\n- Uses `&` operator\n - Shift + 7 on keyboard\n\n:::\n::::\n\n## [Practice: Fundamental Conditionals]{.pink} {.smaller}\n\n![](images/hex-penguins.png){.absolute top=0 left=1100 width=\"12%\" fig-alt=\"palmerpenguins R package hex logo\"}\n\n- We'll use the base R `subset` function with the `peng_df` object\n - If needed, consult the help file for more details (`?subset`)\n\n\\\n\n. . .\n\n- Subset `peng_df` to only Adelie penguins (and assign to a `sub_v1` object)\n - I.e., `species == \"Adelie\"`\n\n\\\n\n. . .\n\n- How many rows does that subset have?\n\n## [More Practice: Fundamental Conditionals]{.pink} {.smaller}\n\n![](images/hex-penguins.png){.absolute top=0 left=1100 width=\"12%\" fig-alt=\"palmerpenguins R package hex logo\"}\n\n- Subset `peng_df` to Adelie _or_ Gentoo penguins\n - Assign this subset to `sub_v2` object\n\n\\\n\n. . .\n\n- Subset `peng_df` to only male Gentoo penguins\n - Assign to `sub_v3` object\n\n\\\n\n. . .\n\n- How many rows does _that_ subset have?\n\n## [Discussion: Conditionals]{.gold} {.smaller}\n\n- We've covered EQUAL, OR, and AND\n - `==`, `|`, or `&`\n\n\\\n\n. . .\n\n- What unanswered questions do you have?\n\n\\\n\n. . .\n\n- What other types of conditional statements would be useful?\n - Think about it in the context of wanting to subset some data\n\n## [Numeric Conditionals]{.orange} {.smaller}\n\n- For numbers, we can use greater/less than conditionals!\n\n\\\n\n. . .\n\n- Greater / less than are expressed as normal\n - `>` and `<`\n\n\\\n\n. . .\n\n- Adding 'or equal to' is done by adding an equal sign\n - `>=` and `<=`\n\n## [Practice: Numeric Conditionals]{.pink} {.smaller}\n\n![](images/hex-penguins.png){.absolute top=0 left=1100 width=\"12%\" fig-alt=\"palmerpenguins R package hex logo\"}\n\n- Subset `peng_df` to only penguins with a bill length greater than 40 mm\n - Assign to `sub_v7`\n\n\\\n\n. . . \n\n- Subset `peng_df` to only penguins with a body mass less than or equal to 4,000 g\n - Assign to `sub_v8`\n\n## [Temperature Check]{.purple}\n\n#### How are you Feeling?\n\n

\n\"Comic-style\n

\n\n## [Upcoming Due Dates]{.blue} {.smaller}\n\n::::{.columns}\n\n:::{.column width=\"50%\"}\n\n### Due before lab\n\n#### (By midnight)\n\n- Muddiest Point \\#2\n- Read [Excuse me, do you have a moment to talk about version control?](https://peerj.com/preprints/3159/)\n\n:::\n\n:::{.column width=\"50%\"}\n\n### Due before lecture\n\n#### (By midnight)\n\n- Homework \\#2\n- Read the British Ecological Society's [Reproducible Code Guide](https://www.britishecologicalsociety.org/wp-content/uploads/2017/12/guide-to-reproducible-code.pdf) (p.20-25)\n\n:::\n\n::::\n\n# [Bonus Conditionals]{.orange}\n\n## [OR with >2 Options]{.orange} {.smaller}\n\n- OR conditionals with many options get cumbersome quickly\n - E.g., `x == 1 | x == 2 | x == 3 | x == 4 …`\n\n\\\n\n. . .\n\n- We can use concatenation and the `%in%` operator to simplify this!\n\n\\\n\n. . .\n\n- `%in%` is effectively \"if any of \\ matches the value\"\n - E.g., `x %in% c(1, 2, 3, 4, …)`\n\n## [Conditionals: NOT]{.orange} {.smaller}\n\n- You can also _exclude_ based on conditions\n - Two different ways of doing this\n\n\\\n\n. . . \n\n- For one / a few options: use `!=` for \"not equal to\"\n - E.g., `x != 10`\n\n\\\n\n. . . \n\n- Can be combined with `%in%` to exclude a set of options\n - E.g., `!x %in% c(...)`\n - Note the exclamation mark is _before_ the object\n\n## [Practice: Advanced Conditionals]{.pink} {.smaller}\n\n![](images/hex-penguins.png){.absolute top=0 left=1100 width=\"12%\" fig-alt=\"palmerpenguins R package hex logo\"}\n\n- Subset `peng_df` is species is any of \"Adelie\", \"Gentoo\", or \"Chinstrap\"\n - Use the `%in%` operator\n\n\\\n\n. . .\n\n- Subset `peng_df` to all islands except \"Dream\"\n", + "supporting": [ + "slides_2a_files" + ], "filters": [ "rmarkdown/pagebreak.lua" ], diff --git a/materials/slides_2a.qmd b/materials/slides_2a.qmd index f3ae9f6..b7a8234 100644 --- a/materials/slides_2a.qmd +++ b/materials/slides_2a.qmd @@ -687,7 +687,7 @@ my_vec[c(1, 1, 1)] ![](images/hex-penguins.png){.absolute top=0 left=1100 width="12%" fig-alt="palmerpenguins R package hex logo"} -- Subset `peng_df` to Adelie _or_ Biscoe penguins +- Subset `peng_df` to Adelie _or_ Gentoo penguins - Assign this subset to `sub_v2` object \