-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day_1_Notes.R
333 lines (213 loc) · 4.06 KB
/
Day_1_Notes.R
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
## Introduction to R:
# Use R as a calculator:----
3 + 5 * 2
(3 + 5) * 2
2/10000
5e3
# syntax for functions:
# function_name(argument)
sin(1)
log(1)
log10(10)
exp(0.5)
# Comparing things:----
1 == 1
1 != 2
1 < 2
1 > 2
1 >= -9
# Variables and Assignment:----
x <- 1/40
x
# shortcut for assignment operator
# mac: option -
# pc: alt -
log(x)
x <- 100
x <- x + 1
x
y <- x * 2
# Vectors:----
1:5
2^(1:5)
x <- 1:5
2^x
# Managing your environment:----
ls()
rm(x)
ls()
# R packages:----
installed.packages()
# install a package:
install.packages("ggplot2")
update.packages("ggplot2")
library("ggplot2")
# Getting Help:----
?ls
vignette("ggplot2")
vignette("colorspace")
help("ggplot2")
```
## Practice with Dataframes:
```
# Practice dataframes:----
cats <- data.frame(coat = c("calico", "black", "tabby"),
weight = c(2.1, 5.0, 3.2),
likes_string = c(1, 0, 1))
cats
# write cats to file:
write.csv(cats, file = "data/feline_data.csv", row.names = FALSE)
rm(cats)
cats
# read in data files:
cats <- read.csv(file = "data/feline_data.csv")
cats
# for tab-delimited data:
# dat <- read.table(file = "PATH TO DATA", header = TRUE, sep = " ")
cats$coat
cats$weight
cats$likes_string
class(cats)
class(cats$coat)
class(cats$weight)
class(cats$likes_string)
# Dimensions:
nrow(cats)
ncol(cats)
dim(cats)
# Math:
cats$weight + 2
cats$weight
cats$weight +
cats$likes_string
cats$weight +
cats$coat
# structure of object
str(cats)
str(cats$weight)
# Factors:
coats <- c('calico', 'black', 'tabby')
coats
str(coats)
str(cats$coat)
CATegories <- factor(coats)
str(CATegories)
coats_2 <- c('calico', 'black', 'tabby', 'black')
str(coats_2)
CAT_2 <- factor(coats_2)
str(CAT_2)
# Challenge:
# 1. read in gapminder data.
gapminder <- read.csv(file = "data/gapminder_data.csv", header = TRUE)
# 2. what is the data type of gapminder?
class(gapminder)
# 3. what is the structure of each variable in the dataset?
str(gapminder$country)
head(gapminder)
dim(gapminder)
install.packages("gapminder")
# Add columns:
cats <- read.csv("data/feline_data.csv")
cats
age <- c(2, 3, 5)
cbind(cats, age)
age <- c(2, 3, 5, 12)
cbind(cats, age)
age <- c(2, 3, 5)
cats <- cbind(cats, age)
cats
# Add rows:
newRow <- list("tortoiseshell", 3.3, 1, 9)
cats <- rbind(cats, newRow)
cats
str(cats$coat)
levels(cats$coat)
levels(cats$coat) <- c(levels(cats$coat), "tortoiseshell")
cats <- rbind(cats, newRow)
cats
# Remove rows:
cats$coat
cats[4, ]
cats[-4, ]
cats[c(-4,-5), ]
cats[4, 3]
cats[ , 2]
cats <- na.omit(cats)
cats
# Realistic Example:
head(gapminder)
summary(gapminder)
tail(gapminder)
set.seed(5)
gapminder[sample(nrow(gapminder),5), ]
# if() and else():----
x <- 8
if(x >= 10){
print("x is greater than or equal to 10")
}
if(x >=10){
print("x id greater than or equal to 10")
} else {
print("x is less than 10")
}
# multiple conditions:
if(x >= 10){
print("x is greater or equal to 10")
} else if(x > 5){
print("x is greater than 5 but less than 10")
} else{
print("x is less than 5")
}
# Example from gapminder data:
# are there any records from 2002?
cats
gapminder[(gapminder$year == 2002), ]
if(nrow(gapminder[(gapminder$year == 2002), ]) >= 1){
print("Data found for 2002")
}
row2002 <- nrow(gapminder[(gapminder$year == 2002), ])
row2002
if(row2002 >= 1){
print("Data found for 2002")
}
# ifelse()
y <- -3
ifelse(y < 0, "y is negative", "y is positive or 0")
y <- 6
ifelse(y < 0, "y is negative", "y is positive or 0")
# For loops:----
# Syntax
# for(iterator in "a set of things"){
# do something
# }
1:10
for(i in 1:10){
print(i)
}
# nesting for loops:
for(i in 1:5){
for(j in c('a','b','c','d','e')){
print(paste(i,j))
}
}
# Make for loops more efficient:
output_vector <- c()
for(i in 1:5){
for(j in c('a','b','c','d','e')){
temp_output <- print(paste(i,j))
output_vector <- c(output_vector, temp_output)
}
}
output_vector
# while loops:
# Syntax
# while(this condition is true){
# do something
# }
z <- 1
runif(z)
set.seed(5)
while(z > 0.1){
z <- runif(1)
cat(z, "\n")
}