-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay1-Intro.R
More file actions
275 lines (132 loc) · 5.2 KB
/
Day1-Intro.R
File metadata and controls
275 lines (132 loc) · 5.2 KB
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
### Limpopo Resilience Lab Virtual R Workshop
### August 18-20, 2021
### Instructor: Max Glines
### original code provided by Rachel Pilla
### DAY 1: Introduction & Basics in R ###
######################################
### Introduction to R and packages ###
######################################
### PTT:
# - what is R?
# - layout/panes of RStudio
# - packages and installing
# - help files
# - using an R script (ALWAYS use script; how to run code; adding comments;
# case sensitive; spaces, parentheses, quotations; carrot in console vs. +)
# - functions, arguments, and objects
# - types of data (numeric, integer, character, logical, complex)
# - data structures (vector, matrix, data frame)
### LIVE CODING (remainder of Day 1)
##########################################
### Basics in R from a prepared script ###
##########################################
# INSTALLING PACKAGES
install.packages(c("dpylr", "tidyr", "ggplot2", "lubridate", "ggpubr"))
#OR
install.packages(c('tidyverse','lubridate','ggpubr'))
# LOADING PACKAGES
library(dplyr)
#OR
library(tidyverse)
# R is a calculator
5 + 4
# WHAT IS AN OBJECT?
# Saving objects in R, and viewing object data
# (Environment Pane; how to name "best" - must begin with letter, caps or lowercase accepted;
# name objects clearly and consistently)
a <- 5 + 4
# Saving a script (file naming...) - do this now
# Scripts can be shared, edited, resaved, copy/pasted, etc. (much like a text editor)
# Style tips for writing code:
# 1) R is case sensitive ("A" does not equal "a")
# 2) Best practice is to put spaces between object, values, commas, etc. (though R does not require this)
# 3) Missing parentheses, commas, or quotation marks cause a vast majority of errors
# 4) Your collaborators and future self will apprecite detailed comments
# 5) Make sure the console has a blue ">" before running (if it has a "+" then it DID NOT FINISH the previous code for some reason)
# Errors and warning messages
# Errors BREAK the code, warnings run it (often not an issue, but keep an eye out that R is doing what you want)
A
c <- b - 3) * 2
cbind(c(1, 2, 3), c(1, 2, 3, 4))
# Basic functions
vector1 <- c(a, b, 1:3)
vector1
# Types of data in R:
# numeric/integer, character/strings, logical, factor (others are less common)
# Types of data structures in R:
# 1) vector - 1D, holds only 1 type of data
# 2) matrix - 2D, holds only 1 type of data
# 3) data frame - 2D, each column can be different type of data
# data frame is very common and useful; other types are generally less common (e.g., lists)
df1 <- data.frame("Column1" = c("A", "B", "C", "D", "E"),
"Column2" = vector1,
"Column3" = c("hi", "my", "name", "is", "Rachel"))
df1
######################
### Getting R Help ###
######################
?mean
?c
??csv
??average
# Google search is a great tool!
# StackOverflow often has really useful answers.
# For example, how do we read in data in a CSV file into R?
#############################
### Importing Data into R ###
#############################
# Create a CSV file for worldwide tropical cyclone data since 2000
# How to save as CSV file and then import to R
# (screen share data file to enter, then save as CSV together)
# Load in data file to R
# "TropicalCycloneData.csv" - pull up to have copied and saved
# OPTION 1 -- selecting file to read in with "read.csv"
file <- file.choose()
storms <- read.csv(file)
# OPTION 2 -- working directories (especially useful if you have lots of data files to import)
getwd()
# point-and-click from "Session" menu OR use "setwd()"
setwd( ... )
storms <- read.csv("TropicalCycloneData.csv")
# Viewing & subsetting data
# (when using square brackets, row ALWAYS comes first)
View(storms)
# select one column by name using "$"
storms$Year
# select one row OR one column using brackets "[]"
storms[5, ]
storms[ , 2]
mean(storms[ , 2])
# select one element/cell
storms[5, 1]
storms$Year[5]
storms$NumberTropicalCyclones[storms$Year == 2005]
# PRACTICE!
# 1) index 5 most recent years of storm data
# 2) find the maximum number of storms
# 3) index the year that had the maximum number of storms
# 1)
# OR
# 2)
# 3)
# OR
# OPEN the file "A7H008YRPK.CSV" in Excel.
# Note there are a few extra rows in the top,
# and we need to tell R to skip them.
# PRACTICE loading another CSV file into R
# choose whichever method to import you'd like!
# import the file "A7H008YRPK.CSV"
beit.bridge <- read.csv("A7H008YRPK.CSV", skip = 7)
head(beit.bridge)
tail(beit.bridge)
str(beit.bridge)
View(beit.bridge)
beit.bridge.clean <- beit.bridge[1:28, ]
################
### PRACTICE ###
################
# 1) import the "A7H008YRPK.CSV",
# remembering to skip the first 7 lines of the file
# 2) calculate the average Level and Flow across all years
# 3) find the minimum and maximum water levels AND which years they occurred
# 4) find the years with the highest and lowest flow