4) Functions and Conditionals
๐ Lecture
You donโt need to be an expert programmer to be a data scientist, but learning more about programming allows you to automate common tasks, and solve new problems with greater ease.
This week weโll discuss how to write basic functions, the notion of compound expressions in R, and an introduction to conditionals (i.e. if-else statements).
๐ Reading
Read chapters 10, 11, and 12 of โR Coding Basicsโ:
๐ฌ Lab
You will get your hands โdirtyโ writing basic functions, and conditional statements.
๐ฏ Objectives
At the end of this week you will be able to:
- Describe the main parts of a function (i.e. anatomy of a function)
- Give a simple example for creating a function
- Explain the concept of an R compound expression
- Write if-else statements to handle simple conditions
- Use if-else statements when writing functions to decide what code to execute
๐ Shiny Friday
The shiny app for this week is a modified version of the default โOld Faithfulโ app.
The main modification has to do with the use of an if-statement to decide whether to display additional statistics on the histogram of waiting
times.
More specifically, we have three new versions:
old-faithful3-average-radio: includes radio buttons to display line of average waiting time
https://github.com/data133/shiny/tree/main/old-faithful3-average-radioold-faithful4-average-checkbox: includes a checkbox to display line of average waiting time
https://github.com/data133/shiny/tree/main/old-faithful4-average-checkboxold-faithful5-statistics: includes an auxiliary function to compute descriptive statistics, and also a checkbox to display descriptive statistics
https://github.com/data133/shiny/tree/main/old-faithful5-statistics
๐ Assignments
- HW2 due this 02/09
- HW3 released on 02/10, due 02/16