diff --git a/.nojekyll b/.nojekyll index f377baf..92bed36 100644 --- a/.nojekyll +++ b/.nojekyll @@ -1 +1 @@ -7299bb24 \ No newline at end of file +a3b605f0 \ No newline at end of file diff --git a/materials/slides_2a.html b/materials/slides_2a.html index 86411a6..fd6352a 100644 --- a/materials/slides_2a.html +++ b/materials/slides_2a.html @@ -852,23 +852,282 @@

How are you Feeling?

Comic-style graph depicting someone's emotional state as they debug code (from initial struggle and defeat to eventual triumph)

-
+

Conditionals

+ +


+

+
+
    +
  • This allows you to write flexible code that can handle any outcome that you can anticipate! +
      +
    • Particularly useful for subsetting data based on the contents of a column
    • +
  • +
+


+

+
+
+
    +
  • These ‘if statements’ are called conditionals
  • +
+


+

+
+
+
    +
  • The answer to a conditional must be either TRUE or FALSE
  • +
+
+
+
+

Fundamentals: EQUAL

+ +


+

+
+
"hello" == "hello"
+
+
[1] TRUE
+
+
+


+

+ +
+
+

Fundamentals: OR

+ +


+

+
+
"hello" == "hello" | 2 == 7
+
+
[1] TRUE
+
+
+


+

+ +
+
+

Fundamentals: AND

+ +


+

+
+
"hello" == "hello" & 2 == 7
+
+
[1] FALSE
+
+
+


+

+
-
-

Conditionals: EQUAL, OR, & AND

+
+

Fundamentals: Summary

+
+
+

EQUAL

+
    +
  • Are two things exactly equal?
  • +
+


+

+
+
"hello" == "hello"
+
+
[1] TRUE
+
+
+


+

+
    +
  • Uses == operator +
      +
    • Just two equal signs
    • +
  • +
+
+

OR

+
    +
  • Are any of these conditions met?
  • +
+


+

+
+
"hello" == "hello" | 2 == 7
+
+
[1] TRUE
+
+
+


+

+
    +
  • Uses | operator +
      +
    • Shift + \ on keyboard
    • +
  • +
+
+

AND

+
    +
  • Are all of the conditions met?
  • +
+


+

+
+
"hello" == "hello" & 2 == 7
+
+
[1] FALSE
+
+
+


+

+
    +
  • Uses & operator +
      +
    • Shift + 7 on keyboard
    • +
  • +
+
+
-
+

Practice: Fundamental Conditionals

+

+
    +
  • We’ll use the base R subset function with the peng_df object +
      +
    • If needed, consult the help file for more details (?subset)
    • +
  • +
+


+

+
+
    +
  • Subset peng_df to only Adelie penguins (and assign to a sub_v1 object) +
      +
    • I.e., species == "Adelie"
    • +
  • +
+
+
+
+

More Practice: Fundamental Conditionals

+

+
    +
  • Subset peng_df to Adelie or Biscoe penguins +
      +
    • Assign this subset to sub_v2 object
    • +
  • +
+


+

+
+
    +
  • Subset peng_df to only male Gentoo penguins +
      +
    • Assign to sub_v3 object
    • +
  • +
+
-
+

Discussion: Conditionals

+
    +
  • We’ve covered EQUAL, OR, and AND +
      +
    • ==, |, or &
    • +
  • +
+


+

+
    +
  • What unanswered questions do you have?
  • +
+


+

+
+
    +
  • What other types of conditional statements would be useful? +
      +
    • Think about it in the context of wanting to subset some data
    • +
  • +
+

Numeric Conditionals

+
    +
  • For numbers, we can use greater/less than conditionals!
  • +
+


+

+
+
    +
  • Greater / less than are expressed as normal +
      +
    • > and <
    • +
  • +
+


+

+
+
+
    +
  • Adding ‘or equal to’ is done by adding an equal sign +
      +
    • >= and <=
    • +
  • +
+

Practice: Numeric Conditionals

+
    +
  • Subset peng_df to only penguins with a bill length greater than 40 mm +
      +
    • Assign to sub_v7
    • +
  • +
+


+

+
+
    +
  • Subset peng_df to only penguins with a body mass less than or equal to 4,000 g +
      +
    • Assign to sub_v8
    • +
  • +
+

Temperature Check

@@ -884,13 +1143,15 @@

Upcoming Due Dates

Due before lab

(By midnight)

Due before lecture

(By midnight)

@@ -900,15 +1161,81 @@

(By midnight)

Bonus Conditionals

-
+

OR with >2 Options

+
    +
  • OR conditionals with many options get cumbersome quickly +
      +
    • E.g., x == 1 | x == 2 | x == 3 | x == 4 …
    • +
  • +
+


+

+
+
    +
  • We can use concatenation and the %in% operator to simplify this!
  • +
+


+

+
+
+
    +
  • %in% is effectively “if any of <this vector> matches the value” +
      +
    • E.g., x %in% c(1, 2, 3, 4, …)
    • +
  • +
+
-
+

Conditionals: NOT

+
    +
  • You can also exclude based on conditions +
      +
    • Two different ways of doing this
    • +
  • +
+


+

+
+
    +
  • For one / a few options: use != for “not equal to” +
      +
    • E.g., x != 10
    • +
  • +
+


+

+
+
+
    +
  • Can be combined with %in% to exclude a set of options +
      +
    • E.g., !x %in% c(...)
    • +
    • Note the exclamation mark is before the object
    • +
  • +
+
+
+
+

Practice: Advanced Conditionals

+
    +
  • Subset peng_df is species is any of “Adelie”, “Gentoo”, or “Chinstrap” +
      +
    • Use the %in% operator
    • +
  • +
+


+

+
+
    +
  • Subset peng_df to all islands except “Dream”
  • +
+
diff --git a/search.json b/search.json index f545de0..0baff0d 100644 --- a/search.json +++ b/search.json @@ -410,42 +410,70 @@ "href": "materials/slides_2a.html#conditionals", "title": "Intro to Data Science", "section": "Conditionals", - "text": "Conditionals" + "text": "Conditionals\n\nYou can write code that runs only if an ‘if statement’ is true\n\nOtherwise that chunk of code is skipped!\n\n\n\n\n\n\nThis allows you to write flexible code that can handle any outcome that you can anticipate!\n\nParticularly useful for subsetting data based on the contents of a column\n\n\n\n\n\n\n\nThese ‘if statements’ are called conditionals\n\n\n\n\n\n\nThe answer to a conditional must be either TRUE or FALSE" }, { - "objectID": "materials/slides_2a.html#conditionals-equal-or-and", - "href": "materials/slides_2a.html#conditionals-equal-or-and", + "objectID": "materials/slides_2a.html#fundamentals-equal", + "href": "materials/slides_2a.html#fundamentals-equal", "title": "Intro to Data Science", - "section": "Conditionals: EQUAL, OR, & AND", - "text": "Conditionals: EQUAL, OR, & AND" + "section": "Fundamentals: EQUAL", + "text": "Fundamentals: EQUAL\n\nAre two things exactly equal?\n\n\n\n\n\"hello\" == \"hello\"\n\n[1] TRUE\n\n\n\n\n\nUses == operator\n\nJust two equal signs" + }, + { + "objectID": "materials/slides_2a.html#fundamentals-or", + "href": "materials/slides_2a.html#fundamentals-or", + "title": "Intro to Data Science", + "section": "Fundamentals: OR", + "text": "Fundamentals: OR\n\nAre any of these conditions met?\n\n\n\n\n\"hello\" == \"hello\" | 2 == 7\n\n[1] TRUE\n\n\n\n\n\nUses | operator\n\nShift + \\ on keyboard" + }, + { + "objectID": "materials/slides_2a.html#fundamentals-and", + "href": "materials/slides_2a.html#fundamentals-and", + "title": "Intro to Data Science", + "section": "Fundamentals: AND", + "text": "Fundamentals: AND\n\nAre all of the conditions met?\n\n\n\n\n\"hello\" == \"hello\" & 2 == 7\n\n[1] FALSE\n\n\n\n\n\nUses & operator\n\nShift + 7 on keyboard" + }, + { + "objectID": "materials/slides_2a.html#fundamentals-summary", + "href": "materials/slides_2a.html#fundamentals-summary", + "title": "Intro to Data Science", + "section": "Fundamentals: Summary", + "text": "Fundamentals: Summary\n\n\nEQUAL\n\nAre two things exactly equal?\n\n\n\n\n\"hello\" == \"hello\"\n\n[1] TRUE\n\n\n\n\n\nUses == operator\n\nJust two equal signs\n\n\n\nOR\n\nAre any of these conditions met?\n\n\n\n\n\"hello\" == \"hello\" | 2 == 7\n\n[1] TRUE\n\n\n\n\n\nUses | operator\n\nShift + \\ on keyboard\n\n\n\nAND\n\nAre all of the conditions met?\n\n\n\n\n\"hello\" == \"hello\" & 2 == 7\n\n[1] FALSE\n\n\n\n\n\nUses & operator\n\nShift + 7 on keyboard" }, { "objectID": "materials/slides_2a.html#practice-fundamental-conditionals", "href": "materials/slides_2a.html#practice-fundamental-conditionals", "title": "Intro to Data Science", "section": "Practice: Fundamental Conditionals", - "text": "Practice: Fundamental Conditionals" + "text": "Practice: Fundamental Conditionals\n\n\nWe’ll use the base R subset function with the peng_df object\n\nIf needed, consult the help file for more details (?subset)\n\n\n\n\n\n\nSubset peng_df to only Adelie penguins (and assign to a sub_v1 object)\n\nI.e., species == \"Adelie\"" + }, + { + "objectID": "materials/slides_2a.html#more-practice-fundamental-conditionals", + "href": "materials/slides_2a.html#more-practice-fundamental-conditionals", + "title": "Intro to Data Science", + "section": "More Practice: Fundamental Conditionals", + "text": "More Practice: Fundamental Conditionals\n\n\nSubset peng_df to Adelie or Biscoe penguins\n\nAssign this subset to sub_v2 object\n\n\n\n\n\n\nSubset peng_df to only male Gentoo penguins\n\nAssign to sub_v3 object" }, { "objectID": "materials/slides_2a.html#discussion-conditionals", "href": "materials/slides_2a.html#discussion-conditionals", "title": "Intro to Data Science", "section": "Discussion: Conditionals", - "text": "Discussion: Conditionals" + "text": "Discussion: Conditionals\n\nWe’ve covered EQUAL, OR, and AND\n\n==, |, or &\n\n\n\n\n\nWhat unanswered questions do you have?\n\n\n\n\n\nWhat other types of conditional statements would be useful?\n\nThink about it in the context of wanting to subset some data" }, { "objectID": "materials/slides_2a.html#numeric-conditionals", "href": "materials/slides_2a.html#numeric-conditionals", "title": "Intro to Data Science", "section": "Numeric Conditionals", - "text": "Numeric Conditionals" + "text": "Numeric Conditionals\n\nFor numbers, we can use greater/less than conditionals!\n\n\n\n\n\nGreater / less than are expressed as normal\n\n> and <\n\n\n\n\n\n\n\nAdding ‘or equal to’ is done by adding an equal sign\n\n>= and <=" }, { "objectID": "materials/slides_2a.html#practice-numeric-conditionals", "href": "materials/slides_2a.html#practice-numeric-conditionals", "title": "Intro to Data Science", "section": "Practice: Numeric Conditionals", - "text": "Practice: Numeric Conditionals" + "text": "Practice: Numeric Conditionals\n\nSubset peng_df to only penguins with a bill length greater than 40 mm\n\nAssign to sub_v7\n\n\n\n\n\n\nSubset peng_df to only penguins with a body mass less than or equal to 4,000 g\n\nAssign to sub_v8" }, { "objectID": "materials/slides_2a.html#temperature-check-1", @@ -459,21 +487,28 @@ "href": "materials/slides_2a.html#upcoming-due-dates", "title": "Intro to Data Science", "section": "Upcoming Due Dates", - "text": "Upcoming Due Dates\n\n\nDue before lab\n(By midnight)\n\nMuddiest Point #x\n\n\nDue before lecture\n(By midnight)\n\nX" + "text": "Upcoming Due Dates\n\n\nDue before lab\n(By midnight)\n\nMuddiest Point #2\nRead Excuse me, do you have a moment to talk about version control?\n\n\nDue before lecture\n(By midnight)\n\nHomework #2\nRead the British Ecological Society’s Reproducible Code Guide (p.20-25)" }, { "objectID": "materials/slides_2a.html#or-with-2-options", "href": "materials/slides_2a.html#or-with-2-options", "title": "Intro to Data Science", "section": "OR with >2 Options", - "text": "OR with >2 Options" + "text": "OR with >2 Options\n\nOR conditionals with many options get cumbersome quickly\n\nE.g., x == 1 | x == 2 | x == 3 | x == 4 …\n\n\n\n\n\n\nWe can use concatenation and the %in% operator to simplify this!\n\n\n\n\n\n\n%in% is effectively “if any of <this vector> matches the value”\n\nE.g., x %in% c(1, 2, 3, 4, …)" }, { "objectID": "materials/slides_2a.html#conditionals-not", "href": "materials/slides_2a.html#conditionals-not", "title": "Intro to Data Science", "section": "Conditionals: NOT", - "text": "Conditionals: NOT\n\n\nProgramming in R for Biologists" + "text": "Conditionals: NOT\n\nYou can also exclude based on conditions\n\nTwo different ways of doing this\n\n\n\n\n\n\nFor one / a few options: use != for “not equal to”\n\nE.g., x != 10\n\n\n\n\n\n\n\nCan be combined with %in% to exclude a set of options\n\nE.g., !x %in% c(...)\nNote the exclamation mark is before the object" + }, + { + "objectID": "materials/slides_2a.html#practice-advanced-conditionals", + "href": "materials/slides_2a.html#practice-advanced-conditionals", + "title": "Intro to Data Science", + "section": "Practice: Advanced Conditionals", + "text": "Practice: Advanced Conditionals\n\nSubset peng_df is species is any of “Adelie”, “Gentoo”, or “Chinstrap”\n\nUse the %in% operator\n\n\n\n\n\n\nSubset peng_df to all islands except “Dream”\n\n\n\nProgramming in R for Biologists" }, { "objectID": "materials/home_week8.html", diff --git a/sitemap.xml b/sitemap.xml index edd679f..15f8289 100644 --- a/sitemap.xml +++ b/sitemap.xml @@ -2,74 +2,74 @@ https://github.com/njlyon0/teach_r-for-biologists/materials/slides_1b.html - 2024-01-23T20:51:01.061Z + 2024-01-23T21:44:13.178Z https://github.com/njlyon0/teach_r-for-biologists/materials/home_week7.html - 2024-01-23T20:50:59.797Z + 2024-01-23T21:44:11.906Z https://github.com/njlyon0/teach_r-for-biologists/materials/slides_2a.html - 2024-01-23T20:50:59.057Z + 2024-01-23T21:44:11.182Z https://github.com/njlyon0/teach_r-for-biologists/materials/home_week8.html - 2024-01-23T20:50:58.125Z + 2024-01-23T21:44:09.982Z https://github.com/njlyon0/teach_r-for-biologists/materials/home_week4.html - 2024-01-23T20:50:57.213Z + 2024-01-23T21:44:09.074Z https://github.com/njlyon0/teach_r-for-biologists/materials/slides_template.html - 2024-01-23T20:50:55.905Z + 2024-01-23T21:44:07.758Z https://github.com/njlyon0/teach_r-for-biologists/materials/home_week2.html - 2024-01-23T20:50:55.161Z + 2024-01-23T21:44:07.010Z https://github.com/njlyon0/teach_r-for-biologists/rubrics/rubric_homework.html - 2024-01-23T20:50:54.065Z + 2024-01-23T21:44:05.926Z https://github.com/njlyon0/teach_r-for-biologists/rubrics/rubric_tutorials.html - 2024-01-23T20:50:53.281Z + 2024-01-23T21:44:05.126Z https://github.com/njlyon0/teach_r-for-biologists/rubrics/rubric_muddiest-point.html - 2024-01-23T20:50:52.125Z + 2024-01-23T21:44:03.974Z https://github.com/njlyon0/teach_r-for-biologists/rubrics/rubric_github.html - 2024-01-23T20:50:53.753Z + 2024-01-23T21:44:05.602Z https://github.com/njlyon0/teach_r-for-biologists/index.html - 2024-01-23T20:50:54.797Z + 2024-01-23T21:44:06.646Z https://github.com/njlyon0/teach_r-for-biologists/materials/home_week3.html - 2024-01-23T20:50:55.569Z + 2024-01-23T21:44:07.418Z https://github.com/njlyon0/teach_r-for-biologists/materials/home_week6.html - 2024-01-23T20:50:56.837Z + 2024-01-23T21:44:08.690Z https://github.com/njlyon0/teach_r-for-biologists/materials/slides_1a.html - 2024-01-23T20:50:57.789Z + 2024-01-23T21:44:09.646Z https://github.com/njlyon0/teach_r-for-biologists/materials/slides_2b.html - 2024-01-23T20:50:58.365Z + 2024-01-23T21:44:10.222Z https://github.com/njlyon0/teach_r-for-biologists/materials/home_week5.html - 2024-01-23T20:50:59.429Z + 2024-01-23T21:44:11.534Z https://github.com/njlyon0/teach_r-for-biologists/materials/home_week1.html - 2024-01-23T20:51:00.173Z + 2024-01-23T21:44:12.266Z