|
| 1 | +--- |
| 2 | +title: "Stat 33A - Lecture 1" |
| 3 | +date: January 27, 2020 |
| 4 | +output: pdf_document |
| 5 | +--- |
| 6 | + |
| 7 | +Lecture notes will usually be posted to bCourses by the end of the day. |
| 8 | + |
| 9 | +Useful links: |
| 10 | + |
| 11 | +* [RWeekly](https://rweekly.org/), news about R |
| 12 | +* [RStudio Cheat Sheets](https://rstudio.com/resources/cheatsheets/) |
| 13 | + |
| 14 | +## Setting Up |
| 15 | + |
| 16 | +R is an interactive language designed for statistical computing. |
| 17 | + |
| 18 | +RStudio (a separate piece of software) is an integrated development |
| 19 | +environment (IDE) for R. |
| 20 | + |
| 21 | +We'll use RStudio in this course. RStudio bundles in R, so you only need to |
| 22 | +install RStudio. |
| 23 | + |
| 24 | +This week's lab will cover setup, but if you want to get ahead, you can download |
| 25 | +and install RStudio Desktop (free edition) from: |
| 26 | + |
| 27 | + <https://rstudio.com/products/rstudio/download/> |
| 28 | + |
| 29 | + |
| 30 | +## R Expressions |
| 31 | +The commands we type and send to R are called **expressions**: |
| 32 | +```{r} |
| 33 | +2 * 20 |
| 34 | +``` |
| 35 | + |
| 36 | + |
| 37 | +## The R Prompt |
| 38 | + |
| 39 | +R has a Read-Eval-Print Loop (REPL): |
| 40 | + |
| 41 | +1. Type an expression at the R prompt and hit the enter key. |
| 42 | +2. R reads the expression. |
| 43 | +3. R evaluates the expression to compute a result. |
| 44 | +4. R prints the result in the console. |
| 45 | +5. R loops back to waiting for you to enter an expression. |
| 46 | + |
| 47 | + |
| 48 | +## R Notebooks |
| 49 | + |
| 50 | +In this course, we'll mostly use R Markdown Notebooks rather than using the R |
| 51 | +prompt directly. Most of the lecture notes and assignments will be R Markdown |
| 52 | +Notebooks. |
| 53 | + |
| 54 | +_The notes in this section are provided by RStudio._ |
| 55 | + |
| 56 | +This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute |
| 57 | +code within the notebook, the results appear beneath the code. |
| 58 | + |
| 59 | +Try executing this chunk by clicking the *Run* button within the chunk or by |
| 60 | +placing your cursor inside it and pressing *Ctrl+Shift+Enter*. |
| 61 | + |
| 62 | +```{r} |
| 63 | +2 + 2 |
| 64 | +``` |
| 65 | + |
| 66 | +Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by |
| 67 | +pressing *Ctrl+Alt+I*. |
| 68 | +```{r} |
| 69 | +
|
| 70 | +``` |
| 71 | + |
| 72 | + |
| 73 | +When you save the notebook, an HTML file containing the code and output will be |
| 74 | +saved alongside it (click the *Preview* button or press *Ctrl+Shift+K* to |
| 75 | +preview the HTML file). |
| 76 | + |
| 77 | +The preview shows you a rendered HTML copy of the contents of the editor. |
| 78 | +Consequently, unlike *Knit*, *Preview* does not run any R code chunks. Instead, |
| 79 | +the output of the chunk when it was last run in the editor is displayed. |
| 80 | + |
| 81 | + |
| 82 | +## Order of Operations |
| 83 | +Arithmetic in R follows an **order of operations**, similar to the order of |
| 84 | +operations you probably learned in a math class. Operations are computed from |
| 85 | +from top to bottom: |
| 86 | + |
| 87 | +1. Parentheses `( )` |
| 88 | +2. Exponentiation `^` |
| 89 | +3. Multiplication `*` and Division `/` |
| 90 | +4. Addition `+` and Subtraction `-` |
| 91 | + |
| 92 | +You can test this out in the R prompt or notebook to get a feel for it: |
| 93 | +```{r} |
| 94 | +5 * 6 - 3 |
| 95 | +``` |
| 96 | + |
| 97 | +R has many more operations besides the ones listed above. |
| 98 | + |
| 99 | +You can see R's complete order of operations with the command: |
| 100 | +```{r} |
| 101 | +?Syntax |
| 102 | +``` |
| 103 | +We'll learn about some of these operations later on. |
| 104 | + |
| 105 | + |
| 106 | +## Attendance Question |
| 107 | +Go to |
| 108 | + |
| 109 | + https://www.yellkey.com/could |
| 110 | + |
| 111 | +to answer. You have until approximately 5pm today. |
| 112 | + |
| 113 | + |
| 114 | +## Using Functions, Part 1 |
| 115 | +In R (and most programming languages), a **function** is a reusable command that |
| 116 | +computes something. |
| 117 | + |
| 118 | +Again, this idea is similar (but not identical) to the functions you probably |
| 119 | +learned about in math class. |
| 120 | + |
| 121 | +If we want the sine of 3, we can write: |
| 122 | +```{r} |
| 123 | +sin(3) |
| 124 | +``` |
| 125 | +When we use a function to compute something, we usually say we "called" the |
| 126 | +function. |
| 127 | + |
| 128 | + |
| 129 | +R has many built in functions for doing math, statistics, and other computing |
| 130 | +tasks. |
| 131 | +```{r} |
| 132 | +sqrt(5) |
| 133 | +``` |
| 134 | + |
| 135 | + |
| 136 | +```{r} |
| 137 | +sum(1, 2, 3) |
| 138 | +``` |
| 139 | + |
| 140 | +```{r} |
| 141 | +3 + 4 |
| 142 | +``` |
| 143 | + |
| 144 | + |
| 145 | +## Getting Help |
| 146 | +R has built-in help files! |
| 147 | + |
| 148 | +You can use the `?` command to get help with a specific function: |
| 149 | +```{r} |
| 150 | +?sin |
| 151 | +``` |
| 152 | +The `?` command should be your first stop when you learn a new function. |
| 153 | + |
| 154 | + |
| 155 | +To access the help for an arithmetic operator, you need to put the name in |
| 156 | +single or double quotes: |
| 157 | +```{r} |
| 158 | +?"+" |
| 159 | +``` |
| 160 | + |
| 161 | + |
| 162 | +Using quotes this way works for ordinary functions as well: |
| 163 | +```{r} |
| 164 | +?"sin" |
| 165 | +``` |
| 166 | + |
| 167 | + |
| 168 | +## Strings |
| 169 | +Sometimes we'll need to treat text as data. In programming languages, a sequence |
| 170 | +of characters (usually textual) is called a **string**. |
| 171 | + |
| 172 | +We already saw how to create a string in R: surround characters with single or |
| 173 | +double quotes. |
| 174 | +```{r} |
| 175 | +"hello class" |
| 176 | +``` |
| 177 | +Note that the quote at the beginning needs to match the quote at the end. |
| 178 | + |
| 179 | + |
| 180 | +## Variables |
| 181 | +You can save the result of a computation by assigning it to a **variable**. A |
| 182 | +variable is a name for a value. |
| 183 | + |
| 184 | +Use `=` or `<-` to assign a value to a variable: |
| 185 | +```{r} |
| 186 | +x <- 35 |
| 187 | +``` |
| 188 | +Either is okay, but choose one and be consistent! |
| 189 | + |
| 190 | + |
| 191 | +Variable names can contain letters or numbers, but can't begin with a number: |
| 192 | +```{r} |
| 193 | +ducks4 = 3 |
| 194 | +``` |
| 195 | + |
| 196 | +We use use variables to: |
| 197 | +* Avoid redundant computations by storing results. |
| 198 | +* Write general expressions, such as `a*x + b` |
| 199 | +* Break code into small steps, so that it's easier to test and understand. |
| 200 | + |
| 201 | +```{r} |
| 202 | +sin(sqrt(2)) + 3 ^ 2 |
| 203 | +sqrt2 = sqrt(2) |
| 204 | +sine = sin(sqrt2) |
| 205 | +
|
| 206 | +``` |
| 207 | + |
| 208 | + |
| 209 | +### Copy-on-Write |
| 210 | +R's variables are **copy-on-write**. |
| 211 | + |
| 212 | +That is, if we assign `x` to `y`: |
| 213 | +```{r} |
| 214 | +x = 3 |
| 215 | +y = x |
| 216 | +``` |
| 217 | + |
| 218 | +And then change `x`: |
| 219 | +```{r} |
| 220 | +x = 12 |
| 221 | +``` |
| 222 | + |
| 223 | +Then `y` remains unchanged: |
| 224 | +```{r} |
| 225 | +y |
| 226 | +``` |
| 227 | + |
| 228 | +Originally, `x` and `y` referred to the same value in memory. |
| 229 | + |
| 230 | +When we changed `x` (a "write"), R automatically copied the original value so |
| 231 | +that `y` remained the same. |
| 232 | + |
| 233 | + |
| 234 | +## Using Functions, Part 2 |
| 235 | +The values we provide as input to a function are called **arguments**. Some |
| 236 | +functions accept exactly one argument: |
| 237 | +```{r} |
| 238 | +tan(3) |
| 239 | +``` |
| 240 | + |
| 241 | +Some functions do not accept any arguments at all. For instance, the |
| 242 | +`sessionInfo()` function, which returns information about your R software: |
| 243 | +```{r} |
| 244 | +sessionInfo() |
| 245 | +``` |
| 246 | + |
| 247 | +Some functions accept multiple arguments: |
| 248 | +```{r} |
| 249 | +# Comments on code start with '#' |
| 250 | +log(12, 2) # logarithm of 12, using base 2 |
| 251 | +``` |
| 252 | + |
| 253 | +Some functions accept any number of arguments: |
| 254 | +```{r} |
| 255 | +sum(1, 2, 3.1) |
| 256 | +``` |
| 257 | + |
| 258 | +Each argument gets assigned to a specific parameter of the function. |
0 commit comments