class: middle, inverse .leftcol30[ <center> <img src="https://github.com/emse-p4a-gwu/emse-p4a-gwu.github.io/blob/master/images/logo.png?raw=true" width=250> </center> ] .rightcol70[ # Week 2: .fancy[Functions & Packages] ###
EMSE 4571 / 6571: Intro to Programming for Analytics ###
John Paul Helveston ###
January 25, 2024 ] --- class: inverse # Quiz 1
−
+
10
:
00
.leftcol[ ## Write your name on the quiz! ## Rules: - Work alone; no outside help of any kind is allowed. - No calculators, no notes, no books, no computers, no phones. ] .rightcol[ <br> <center> <img src="https://github.com/emse-p4a-gwu/2022-Spring/raw/main/images/quiz_doge.png" width="400"> </center> ] --- class: inverse, center, middle # Where is all this going? --- class: inverse, middle # Week 2: .fancy[Functions & Packages] ## 1. Functions ## 2. Manipulating data types ### BREAK ## 3. External packages 📦 ## 4. Polya's problem solving technique --- class: inverse, middle # Week 2: .fancy[Functions & Packages] ## 1. .orange[Functions] ## 2. Manipulating data types ### BREAK ## 3. External packages 📦 ## 4. Polya's problem solving technique --- # Funtions take this form: ## `name(argument)` -- ```r sqrt(225) ``` ``` #> [1] 15 ``` -- Not every function has an argument: ```r date() ``` ``` #> [1] "Wed Jan 24 13:44:50 2024" ``` --- ### Some functions have multiple arguments: ```r round(3.1415, 2) ``` ``` #> [1] 3.14 ``` -- ### Arguments have names too: ```r round(x = 3.1415, digits = 2) ``` ``` #> [1] 3.14 ``` -- ### If you don't include all arguments, default values will be used: ```r round(x = 3.1415) ``` ``` #> [1] 3 ``` --- # For arguments, use "`=`" , not "`<-`" -- .leftcol[ ### `=`<br>Arguments are "local" to the function ```r round(x = 3.1415, digits = 2) ``` ``` #> [1] 3.14 ``` ```r x ``` ``` Error: object 'x' not found ``` ] -- .rightcol[ ### `<-`<br>Arguments also get created "globally" ```r round(x <- 3.1415, digits <- 2) ``` ``` #> [1] 3.14 ``` ```r x ``` ``` #> [1] 3.1415 ``` ```r digits ``` ``` #> [1] 2 ``` ] --- # Use `?` to get help ```r ?round() ``` -- ``` Rounding of Numbers Description Usage ceiling(x) floor(x) trunc(x, ...) round(x, digits = 0) signif(x, digits = 6) Arguments x a numeric vector. Or, for round and signif, a complex vector. digits integer indicating the number of decimal places (round) or significant digits (signif) to be used. Negative values are allowed (see ‘Details’). ``` --- # Combining functions -- You can use functions as arguments to other functions: ```r round(sqrt(7), digits = 2) ``` ``` #> [1] 2.65 ``` -- What do you think this will return: ```r sqrt(1 + abs(-8)) ``` -- ``` #> [1] 3 ``` --- ## Frequently used **math** functions .font90[ Function | Description | Example input | Example output ---------- | ------------------|------------------|--------------- `sqrt()` | Square root | `sqrt(64)` | 8 `round(x, digits=0)` | Round `x` to the `digits` decimal place | `round(3.1415, digits=2)` | 3.14 `floor(x)` | Round `x` **down** the nearest integer | `floor(3.9)` | 3 `ceiling(x)` | Round `x` **up** the nearest integer | `ceiling(3.1)` | 4 `abs()` | Absolute value | `abs(-42)` | 42 `min()` | Minimum value | `min(1, 2, 3)` | 1 `max()` | Maximum value | `max(1, 2, 3)` | 3 ] --- class: inverse
−
+
05
:
00
# Your turn .leftcol[ Consider the following code blocks: Block 1: ```r val <- abs(x <- sqrt(10)) result <- round(val, digits <- sqrt(10)) answer <- x*digits answer ``` Block 2: ```r val <- sqrt(abs(min(-42, -64, 81))) result <- floor(y = min(val, log(10))) answer <- result*val answer ``` ] .rightcol[ Now follow these steps: 1. Don't run the code (yet)! 2. Write down out what you expect R will return when these lines are run in sequence. 3. Compare your expectations with each other. 4. Run the code and compare the results with your expectations. ] --- class: inverse, middle # Week 2: .fancy[Functions & Packages] ## 1. Functions ## 2. .orange[Manipulating data types] ### BREAK ## 3. External packages 📦 ## 4. Polya's problem solving technique --- # Use these patterns: -- .leftcol40[ ### Convert type of `x`: ###`as.______(x)` <br> ### Check type of `x`: ### `is.______(x)` ] -- .rightcol60[ ### Replace "`______`" with: - ### `character` - ### `logical` - ### `numeric` / `double` / `integer` ] --- ## Convert type with `as.______(x)` -- .leftcol[ ### Convert **numeric** types: ```r as.numeric("3.1415") ``` ``` #> [1] 3.1415 ``` ```r as.double("3.1415") ``` ``` #> [1] 3.1415 ``` ```r as.integer("3.1415") ``` ``` #> [1] 3 ``` ] -- .rightcol[ ### Convert **non-numeric** types: ```r as.character(3.1415) ``` ``` #> [1] "3.1415" ``` ```r as.logical(3.1415) ``` ``` #> [1] TRUE ``` ] --- # A few notes on converting types -- .leftcol[ ### Converting any number to a logical returns `TRUE` except for `0` ```r as.logical(7) ``` ``` #> [1] TRUE ``` ```r as.logical(0) ``` ``` #> [1] FALSE ``` ] -- .rightcol[ ### `TRUE = 1`, `FALSE = 0`: ```r as.numeric(TRUE) ``` ``` #> [1] 1 ``` ```r as.numeric(FALSE) ``` ``` #> [1] 0 ``` ] --- # A few notes on converting types ### Not everything can be converted. -- .leftcol[ ```r as.numeric('7') # Works ``` ``` #> [1] 7 ``` ```r as.numeric('foo') # Doesn't work ``` ``` #> [1] NA ``` ] --- # A few notes on converting types ### `as.integer()` is the same as `floor()`: .leftcol[ ```r as.integer(3.14) ``` ``` #> [1] 3 ``` ```r as.integer(3.99) ``` ``` #> [1] 3 ``` ] --- ## Check type with `is.______(x)` -- .leftcol[ ### Checking **numeric** types: ```r is.numeric(3.1415) ``` ``` #> [1] TRUE ``` ```r is.double(3.1415) ``` ``` #> [1] TRUE ``` ```r is.integer(3.1415) ``` ``` #> [1] FALSE ``` ] -- .rightcol[ ### Checking **non-numeric** types: ```r is.character(3.1415) ``` ``` #> [1] FALSE ``` ```r is.logical(3.1415) ``` ``` #> [1] FALSE ``` ] --- # Integers are weird -- ```r is.integer(7) ``` ``` #> [1] FALSE ``` ...because R thinks `7` is really `7.0` -- <br> **To check if a number is an integer _in value_:** ```r 7 == as.integer(7) ``` ``` #> [1] TRUE ``` --- class: inverse
−
+
08
:
00
# Your turn Consider the following code (don't run it): ```r number <- as.logical(as.numeric('3')) character <- is.character(typeof(7)) true <- as.logical("FALSE") false <- as.logical(as.numeric(TRUE)) ! (number == character) & (true | false) | (number & false) ``` Now follow these steps: 1. Don't run the code (yet)! 2. Write down out what you expect R will return when these lines are run in sequence. 3. Compare your expectations with each other. 4. Run the code and compare the results with your expectations. --- class: inverse, center # .fancy[Intermission] <br> ## Stand up, stretch, move around!
−
+
05
:
00
--- class: center, middle, inverse # .font150[.fancy[.orange[Tip of the week]]<br><br>What's with that .RData file?] --- class: center ## Don't save the `.RData` file on exit -- .leftcol30[ <center> <img src="images/save.png" width=100%> </center> ] -- .rightcol70[ **Tools -> Global Options** <center> <img src="images/RData.png" width=500> </center> ] --- class: inverse, middle # Week 2: .fancy[Functions & Packages] ## 1. Functions ## 2. Manipulating data types ### BREAK ## 3. .orange[External packages] 📦 ## 4. Polya's problem solving technique --- background-color: #fff class: center # >20,000 [packages](https://cran.r-project.org/web/packages/available_packages_by_name.html) on the [CRAN](https://cran.r-project.org/) <img src="figs/unnamed-chunk-36-1.png" width="792" style="display: block; margin: auto;" /> --- ## Installing: `install.packages("packagename")` -- <br> ### Package name must be in quotes ```r install.packages("packagename") # This works install.packages(packagename) # This doesn't work ``` -- <br> ### **You only need to install a package once!** --- ## Loading: `library(packagename)` -- <br> ### Package name _doesn't_ need to be in quotes ```r library("packagename") # This works library(packagename) # This also works ``` -- <br> ### **You need to _load_ the package every time you use it!** --- background-color: #fff class: center # Installing vs. Loading <center> <img src="images/package_lightbulb.png" width=1000> </center> --- ## Example: **wikifacts** Install the [Wikifacts](https://github.com/keithmcnulty/wikifacts) package, by Keith McNulty: ```r install.packages("wikifacts") ``` -- Load the package: ```r library("wikifacts") # Load the library ``` -- Use one of the package functions ```r wiki_randomfact() ``` ``` #> [1] "Here's some news from 10 October 2022. Ethiopian Yalemzerf Yehualaw and Kenyan Amos Kipruto win the London Marathon women's and men's races. (Courtesy of Wikipedia)" ``` --- ## Example: **wikifacts** Now, restart your RStudio session: > Session -> Restart R -- Try using the package function again: ```r wiki_randomfact() ``` ``` #> Error in wiki_randomfact(): could not find function "wiki_randomfact" ``` --- # Using only _some_ package functions -- <br> ### Functions can be accessed with this pattern: `packagename::functionname()` -- ```r wikifacts::wiki_randomfact() ``` ``` #> [1] "Did you know that in 1956, German civil servant Erica Pappritz co-wrote a book on etiquette which included sections on correct odour and on how Bonn diplomats liked to carry umbrellas? (Courtesy of Wikipedia)" ``` --- # Learn more about a package: ## `help(package = 'packagename')` -- <br> ```r help(package = 'wikifacts') ``` --- class: inverse
−
+
10
:
00
# Your turn .leftcol60[ 1. Install the `TurtleGraphics` package. 2. Restart RStudio. 3. Load the `TurtleGraphics` package. 4. Use the `turtle_init()` function to create a turtle. 5. Use `help(package = "TurtleGraphics")` to learn about other functions to control your turtle. 6. Try drawing this shape with your turtle<br>(hint: the length of each line is `50` units). 7. Compare your results and code with each other. ] .rightcol40[ <img src="images/turtle_example.png"> ] --- class: inverse, middle # Week 2: .fancy[Functions & Packages] ## 1. Functions ## 2. Manipulating data types ### BREAK ## 3. External packages 📦 ## 4. .orange[Polya's problem solving technique] --- # [Polya](https://en.wikipedia.org/wiki/George_P%C3%B3lya)’s Problem Solving Technique <br> -- ### **Step 1**: Understand the problem -- ### **Step 2**: Devise a plan -- ### **Step 3**: Carry out the plan -- ### **Step 4**: Check your work --- # Polya’s Problem Solving Technique .leftcol[ ### .red[**Step 1**: Understand the problem] ### **Step 2**: Devise a plan ### **Step 3**: Carry out the plan ### **Step 4**: Check your work ] -- .rightcol[ - Seems obvious (easy to overlook) - Restate the problem in your own words - Draw a figure - What information do you _have_? - What information do you _need_? ] --- # Polya’s Problem Solving Technique .leftcol[ ### **Step 1**: Understand the problem ### .red[**Step 2**: Devise a plan] ### **Step 3**: Carry out the plan ### **Step 4**: Check your work ] -- .rightcol[ - Do you know a related problem? - Look at the unknown! - Guess and check - Eliminate possibilities - Consider special cases - Work backwards ] --- # Polya’s Problem Solving Technique .leftcol[ ### **Step 1**: Understand the problem ### **Step 2**: Devise a plan ### .red[**Step 3**: Carry out the plan] ### **Step 4**: Check your work ] -- .rightcol[ - (this is where you write code) - **Be patient** - Stick to the plan... - ...until the plan fails, then change your plan - Error message != plan has failed ] --- # Polya’s Problem Solving Technique .leftcol[ ### **Step 1**: Understand the problem ### **Step 2**: Devise a plan ### **Step 3**: Carry out the plan ### .red[**Step 4**: Check your work] ] -- .rightcol[ - Seems obvious (easy to overlook) - Check intermediate values - Can you derive the solution differently? ] --- class: inverse
−
+
10
:
00
# Polya practice: What's your degree worth? .leftcol35[ 1. Understand the problem 2. Devise a plan 3. Carry out the plan 4. Check your work ] .rightcol65[ In the U.S., the average annual salary of a high school graduate is [$35,256](https://smartasset.com/retirement/the-average-salary-by-education-level), and the average salary of a GW graduate is <a href="https://www.payscale.com/research/US/School=George_Washington_University_(GWU)/Salary">$76,151</a>. However, GW grads pay an average of $70,000 / year (tuition + fees + housing) for 4 years for their degree, and high school grads are working that entire time. Assuming immediate employment after graduation, .orange[how many years after graduating will the GW grad need to work until their net income (salary minus cost of education) surpasses that of the average high school graduate?] (NOTE: This is a _very_ rough estimate - we're assuming away interest rates, inflation, promotions, etc.) ] --- class: inverse
−
+
10
:
00
# Polya practice: Should you buy a Hybrid car? .leftcol35[ 1. Understand the problem 2. Devise a plan 3. Carry out the plan 4. Check your work ] .rightcol65[ Kevin is deciding between purchasing a Toyota Prius, which sells for $27,600, and a Toyota Camry, which sells for $24,000. He knows he can get an average fuel economy of 55 miles per gallon (mpg) in the Prius but only 28 mpg in the Camry on average. He also knows that he typically drives 12,000 miles each year, and the average price of gasoline is $3.00 / gallon. .orange[How long (in years) would Kevin have to drive the Prius for the money he saves in fuel savings to be greater than the price premium compared to the Camry?] ]