-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblank_notes.Rmd
210 lines (122 loc) · 3.41 KB
/
blank_notes.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
---
title: "Stat 33A - Lecture Notes 1"
date: August 23, 2020
output: pdf_document
---
The R Interface
===============
You can interact with and control R by typing code into the prompt.
The R prompt is a Read-Eval-Print Loop (__REPL__):
1. R waits for you to type in some code and press `Enter`.
2. R reads the code.
3. R __evaluates__ the code to compute a result.
4. R prints the result in the console.
5. R loops back around to waiting for you to enter an expression.
A single piece of code is called an __expression__.
```{r}
```
## Getting Help
R has built-in help files.
You can use the `?` command to get help with a specific function:
```{r}
```
The `?` command should be your first stop when you learn a new function.
To get help with arithmetic commands, you must put the symbol in single or
double quotes:
```{r}
```
This works for ordinary functions as well:
```{r}
```
You can use the `??` command to search the help files:
```{r}
```
## Order of Operations
Arithmetic in R follows an __order of operations__, similar to the order of
operations you probably learned in a math class.
The order from first to last is:
1. Parentheses `( )`
2. Exponentiation `^`
3. Multiplication `*` and Division `/`
4. Addition `+` and Subtraction `-`
Try this out in the R prompt to get a feel for it:
```{r}
```
R has many more operations besides the ones listed above.
You can see R's complete order of operations with the command:
```{r}
```
We'll learn about some of these operations later on.
Variables
=========
A __variable__ is a name for a value.
Use `=` or `<-` to assign a value to a variable:
```{r}
```
Either is okay, but choose one and be consistent!
The value can be the result of a computation:
```{r}
```
Variable names can contain letters or numbers, but can't begin with a number:
```{r}
```
With variables, you can:
* Reuse the result of a computation
* Write general expressions, such as `a*x + b`
* Break code into small steps, so that it's easier to test and understand.
```{r}
```
## Copy-on-Write
In R, variables are __copy-on-write__.
That is, if we assign `x` to `y`:
```{r}
```
And then change `x`:
```{r}
```
Then `y` remains unchanged:
```{r}
```
Originally, `x` and `y` referred to the same value in memory.
When we changed `x` (a "write"), R automatically copied the original value so
that `y` remained the same.
Calls
=====
A **function** is a reusable command that computes something.
The idea is similar to the functions you probably learned about in math class.
To compute the sine of 3, you can write:
```{r}
```
Code that uses a function is said to __call__ that function.
The part of the code where the function is used is also called a __call__.
R has many built in functions for doing math, statistics, and other computing
tasks.
```{r}
```
## Parameters
A function's inputs are called __parameters__.
Parameters are listed in each function's help file:
```{r}
```
## Arguments
When you call a function, the values you assign as input are called
__arguments__.
Some functions accept exactly one argument:
```{r}
```
Some functions do not require any arguments at all:
```{r}
```
Some functions accept multiple arguments:
```{r}
```
Some functions accept any number of arguments:
```{r}
```
You can set arguments based on the order of the parameters:
```{r}
```
Or by using the names of the parameters:
```{r}
```
A function has parameters; a call has arguments!