-
Notifications
You must be signed in to change notification settings - Fork 13
/
class_1.Rmd
186 lines (144 loc) · 2.9 KB
/
class_1.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
---
title: "Live Coding"
author: "Julia Gallucci"
date: "2023-12-11"
output: html_document
---
Class 1
Lesson 00,01,02
Get working directory
```{r}
getwd() # get working directory
```
Set working directory (NOT recommended method)
```{r}
# setwd("/Users/as/Desktop/DSI-IntroR")
```
Set working directory (Recommended method)
- Session -> Set working directory -> choose directory
- Or use files tab
A variable/ R object
As mentioned, the top right quadrant is where variables you made are stored. lets make a variable!
```{r}
exampleVariable <- 1 # <- is the assignment operator
exampleVariable = 1 # not recommended
exampleVariable # autoprinting
```
you can remove variables so they no longer appear in your environment
```{r}
rm(exampleVariable)
```
You can comment outside of chunks, or within chunks using the #
```{r}
# This is a new comment.
```
R is case sensitive
```{r}
exampleVariable <- 1
examplevariable
exampleVariable
exampleVariable2 <- 2
rm(exampleVariable, exampleVariable2)
```
You can also overwrite variables or 'reassign' them
```{r}
exampleVariable <- 1
exampleVariable
exampleVariable <- 10 # dynamic variable typing
exampleVariable
exampleVariable <- "a"
exampleVariable
```
Variable convention
you can call your variable in the console by simply typing in the variable name
Object names camelCase styling
```{r}
exampleVariable
```
Object names snake_case styling
```{r}
example_variable
```
Object names dot.style not recommended
```{r}
example.variable
```
R has reserved words
```{r}
NA # not available
NaN # not a number
TRUE
FALSE
T
F
# don't use these to create objects
```
Write and run your own variable!
```{r}
my_name <- "Julia"
todays_date <- "December 11, 2023"
```
Mathematical operations
+ add
- subtract
* multiplication
/ division
Let’s pretend we want to write some code, that performs basic math operations.
I.e., we want to code 2+2
```{r}
2 + 2
```
Write and run your own basic addition in your script!
We can do something similar, directly in the console (type 2 + 2 in the console!)
```{r}
```
Built-in functions; originally a statistical language
```{r}
mean()
sqrt()
seq()
```
Structure of a function
functionName(argumentName)
args, return values
this gives return values
```{r}
nums <- seq(1,3)
mean(nums)
```
Help documentation
example from help documentation
```{r}
?mean
help(mean)
```
In-class exercises
Lesson 01 slide 43
1. load our libraries
```{r}
library(tidyverse)
library(faraway)
```
2. load the datatset "broccoli"
```{r}
data("broccoli")
broccoli
```
In-class exercises
Lesson 02
1. Create toy dataset
```{r}
toy_data <- data.frame(ID = 1:4,
name = c("Abe", "Becca", "Calvin", "Danica"),
age = c(27,32,63,55), membership = c("yes","no","yes","yes"))
toy_data
```
2. Create a reprex
```{r}
#install.packages("reprex")
library(reprex)
reprex({
toy_data$Name
})
```
# End of class 11 December