Skip to content

Commit 618c8f6

Browse files
committed
Import STAT 33A lecture notes.
0 parents  commit 618c8f6

29 files changed

+7188
-0
lines changed

stat33a/lecture/01.27/notes.Rmd

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
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.

stat33a/lecture/01.27/notes.pdf

224 KB
Binary file not shown.

stat33a/lecture/01.27/rsession.txt

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
R version 3.6.2 (2019-12-12) -- "Dark and Stormy Night"
3+
Copyright (C) 2019 The R Foundation for Statistical Computing
4+
Platform: x86_64-pc-linux-gnu (64-bit)
5+
6+
R is free software and comes with ABSOLUTELY NO WARRANTY.
7+
You are welcome to redistribute it under certain conditions.
8+
Type 'license()' or 'licence()' for distribution details.
9+
10+
Natural language support but running in an English locale
11+
12+
R is a collaborative project with many contributors.
13+
Type 'contributors()' for more information and
14+
'citation()' on how to cite R or R packages in publications.
15+
16+
Type 'demo()' for some demos, 'help()' for on-line help, or
17+
'help.start()' for an HTML browser interface to help.
18+
Type 'q()' to quit R.
19+
20+
R version 3.6.2 (2019-12-12) -- Dark and Stormy Night
21+
Type 'citation()', 'contributors()', or 'license()' for more information
22+
23+
Libraries:
24+
* /home/nick/.config/R/library
25+
* /usr/lib/R/library
26+
27+
> 3 + 5
28+
[1] 8
29+
> 3 - 5
30+
[1] -2
31+
> 3 * 2
32+
[1] 6
33+
> 4 / 6
34+
[1] 0.6666667
35+
> 2 * 20
36+
[1] 40
37+
> 5 * 6 - 3
38+
[1] 27
39+
> ?Syntax
40+
> sin(3)
41+
[1] 0.14112
42+
> cos(3)
43+
[1] -0.9899925
44+
> sqrt(5)
45+
[1] 2.236068
46+
> sum(1, 2, 3)
47+
[1] 6
48+
> 3 + 4
49+
[1] 7
50+
> ?sin
51+
> ?+
52+
.
53+
54+
> ?"+"
55+
> ?"sin"
56+
> "hello class"
57+
[1] "hello class"
58+
> x = 35
59+
> x
60+
[1] 35
61+
> y
62+
Error: object 'y' not found
63+
> 4ducks = 3
64+
Error: unexpected symbol in "4ducks"
65+
> ducks4 = 3
66+
> sin(sqrt(2)) + 3 ^ 2
67+
[1] 9.987766
68+
>

0 commit comments

Comments
 (0)