-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProject2_RCode.R
296 lines (158 loc) · 7.96 KB
/
Project2_RCode.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
library(tidyverse)
library(R.utils)
library(lubridate)
if(!file.exists("./Proj2data")){dir.create("./Proj2data")} # Check to see if the subdirectory for storing our data exists.
# If it does not, create it.
fileUrl <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2" # Set the DL link url.
if(!file.exists("./Proj2data/Stormdata.csv.bs2")) {download.file(fileUrl,destfile="./Proj2data/StormData.csv.bz2",method="curl")} # Actually download the file.
gunzip(filename = "./Proj2data/StormData.csv.bz2", destname = "./Proj2data/StormData.csv",
skip = TRUE, ext = "bz2")
stormdata <- as_tibble(read.csv(file = "./Proj2data/StormData.csv", stringsAsFactors = FALSE, strip.white = TRUE))
fileUrl <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2" # Set the DL link url.
download.file(fileUrl,destfile="./Proj2data/StormData.csv.bz2",method="curl") # Actually download the file.
gunzip(filename = "./Proj2data/StormData.csv.bz2", destname = "./Proj2data/StormData.csv",
skip = TRUE, ext = "bz2")
stormdata <- as_tibble(read.csv(file = "./Proj2data/StormData.csv", stringsAsFactors = FALSE, strip.white = TRUE))
# Testing spead of read.csv vs fread for a huge datafile
Sys.time()
# Time: 17 seconds
Sys.time()
stormdata2 <- as_tibble(data.table::fread(file = "./Proj2data/StormData.csv", stringsAsFactors = FALSE, strip.white = TRUE))
Sys.time()
# Time: 4 seconds
str(stormdata2)
stormdata == stormdata2
# Yep, it tracks, so for the RMD, use fread. But as_tibble because data table format gave me super weird errors that one time.
glimpse(stormdata)
str(stormdata)
max(stormdata$FATALITIES)
max(stormdata$INJURIES)
stormdata[stormdata$FATALITIES == 583,]
stormdata[stormdata$INJURIES == 1700,]
glimpse(stormdata)
# Clever: activity$date <- as.Date(activity$date)
stormdata$BGN_DATE <- as.Date(stormdata$BGN_DATE)
#Error
?lubridate
# If the dates are in the format i.e. 4/18/1950 0:00:00, I think I want the lubridate command:
stormtest <- stormdata[1:1000,]
stormtest$BGN_DATE <- mdy_hms(stormtest$BGN_DATE)
stormtest$BGN_DATE <- as.Date(stormtest$BGN_DATE)
glimpse(stormtest)
stormdata$REFNUM
Katrina <- filter(stormdata, REFNUM == "577615")
Katrina$REMARKS
# Number of fatalities = 1097
max(stormdata$FATALITIES)
max(stormdata$INJURIES)
maxfatal <- stormdata[stormdata$FATALITIES == max(stormdata$FATALITIES),]
maxfatal$REMARKS
maxhurt <- stormdata[stormdata$INJURIES == 1700,]
max
stormdata[stormdata$REFNUM == 577615,36]
# Fixing hurricane katrina's fatalities...
match("FATALITIES", colnames(stormdata))
match("INJURIES", colnames(stormdata))
stormdata[stormdata$REFNUM == 577615,23] <- 1097
stormdata[stormdata$REFNUM == 577615,23]
max(stormdata$FATALITIES)
# Okay let's stop here.
# Install cacher from a local file:
if(!file.exists("./Proj2data/cacher_1.1-2.tar.gz")) {
download.file("https://cran.r-project.org/src/contrib/Archive/cacher/cacher_1.1-2.tar.gz",destfile="./Proj2data/cacher_1.1-2.tar.gz",method="curl")
} else {
install.packages("./Proj2data/cacher_1.1-2.tar.gz", repos=NULL, type= "both")
}
?install.packages
fileUrl <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2" # Set the DL link url.
download.file(fileUrl,destfile="./Proj2data/StormData.csv.bz2",method="curl") # Actually download the file.
install.packages("https://cran.r-project.org/src/contrib/Archive/cacher/cacher_1.1-2.tar.gz", repos = NULL, type ="both")
install.packages("./Proj2data/cacher_1.1-2.tar.gz", repos=NULL, type= "both")
library(cacher)
# Figuring out what years are in the data set:
whatismag <- stormdata[stormdata$MAG != 0, ]
unique(whatismag$EVTYPE)
tornados <- whatismag[whatismag$EVTYPE == "TORNADO",]
?substr
substr(stormdata[1,2],1,2)
year(stormdata[1,2])
stormdata[1:10,2]
is.Date(stormdata[1,2])
as.Date(stormdata[1,2])
as.Date.POSIXlt(stormdata[1,2])
parse_date_time(stormdata[1,2],"ymd")
as.Date.POSIXlt(stormtest$BGN_DATE)
?as.Date.POSIXlt()
x <- ymd("2012-03-26")
year(x)
x <- stormdata[1,2]
x
year(x)
stormtest2 <- stormtest
stormtest2$BGN_DATE <- as.Date(stormtest2$BGN_DATE)
stormtest2[1,2]
glimpse(stormtest2)
year(stormtest2$BGN_DATE)
stormtest2$BGN_DATE <- year(stormtest2$BGN_DATE)
glimpse(stormtest2)
stormtest2$BGN_DATE <- as.Date(stormtest2$BGN_DATE, origin = "0000")
stormtest2$BGN_DATE
unique(stormtest2$BGN_DATE)
?as.Date
unlist(stormtest2[1,2])
stormdates <- stormdata[,2]
glimpse(stormdates)
stormdates$BGN_DATE <- as.Date(stormdates$BGN_DATE)
stormyears <- year(stormdates$BGN_DATE)
unique(stormyears)
# Figuring out how to calculate PROPDMG properly. PROPDMG = 25, CROPDMG = 27
match("PROPDMG", colnames(stormdata))
match("CROPDMG", colnames(stormdata))
range(stormdata$PROPDMG)
summary(stormdata$PROPDMG)
hist(stormdata$PROPDMG)
?hist
hist(stormdata$PROPDMG, xlim = "1000")
?hist
lowpropdamage <- stormdata[stormdata$PROPDMG %in% c(1:1000),]
hist(lowpropdamage$PROPDMG)
stormtest$EVTYPE <- tolower(stormtest$EVTYPE)
stormdata2 <- stormdata
glimpse(stormdata2)
stormdata2$EVTYPE <- tolower(stormdata2$EVTYPE)
fullgroups <- group_by(stormdata2, EVTYPE)
unique(stormdata2$EVTYPE)
stormhealth <- filter(fullgroups,FATALITIES > 0 | INJURIES > 0) %>%
summarise(FATALITIES = sum(FATALITIES), INJURIES = sum(INJURIES)) %>%
arrange(desc(FATALITIES + INJURIES))
stormhealth
dictionary <- c("Astronomical Low Tide","Avalanche","Blizzard","Coastal Flood","Cold/Wind Chill","Debris Flow","Dense Fog","Dense Smoke","Drought","Dust Devil","Dust Storm","Excessive Heat","Extreme Cold/Wind Chill","Flash Flood","Flood","Freezing Fog","Frost/Freeze","Funnel Cloud","Hail","Heat","Heavy Rain","Heavy Snow","High Surf","High Wind","Hurricane/Typhoon","Ice Storm","Lakeshore Flood","Lake-Effect Snow","Lightning","Marine Hail","Marine High Wind","Marine Strong Wind","Marine Thunderstorm Wind","Rip Current","Seiche","Sleet","Storm Tide","Strong Wind","Thunderstorm Wind","Tornado","Tropical Depression","Tropical Storm","Tsunami","Volcanic Ash","Waterspout","Wildfire","Winter Storm","Winter Weather")
dictionary <- tolower(dictionary)
stormdata3 <- stormdata2
install.packages("stringdist")
library(stringdist)
stormdata3$EVTYPE_48 <- dictionary[amatch(stormdata3$EVTYPE,dictionary,method="lv", maxDist=20)]
length(unique(stormdata2$EVTYPE))
length(unique(stormdata3$EVTYPE_48))
fullgroups48 <- group_by(stormdata3, EVTYPE_48)
stormhealth <- filter(fullgroups48,FATALITIES > 0 | INJURIES > 0) %>%
summarise(FATALITIES = sum(FATALITIES), INJURIES = sum(INJURIES)) %>%
arrange(desc(FATALITIES + INJURIES))
colnames(stormhealth)
gatheredhealth <- gather(stormhealth,EVTYPE_48, VALUE, FATALITIES:INJURIES)
?gather
# No, that's stupid
rm(gatheredhealth)
# Full code for messing around to fix EVTypes for humans:
dictionary <- c("Astronomical Low Tide","Avalanche","Blizzard","Coastal Flood","Cold/Wind Chill","Debris Flow","Dense Fog","Dense Smoke","Drought","Dust Devil","Dust Storm","Excessive Heat","Extreme Cold/Wind Chill","Flash Flood","Flood","Freezing Fog","Frost/Freeze","Funnel Cloud","Hail","Heat","Heavy Rain","Heavy Snow","High Surf","High Wind","Hurricane","Typhoon","Ice Storm","Lakeshore Flood","Lake-Effect Snow","Lightning","Marine Hail","Marine High Wind","Marine Strong Wind","Marine Thunderstorm Wind","Rip Current","Sleet","Storm Tide","Strong Wind","Thunderstorm Wind","Tornado","Tropical Depression","Tropical Storm","Tsunami","Volcanic Ash","Waterspout","Wildfire","Winter Storm","Winter Weather")
dictionary <- tolower(dictionary)
summed_human_events$EVTYPE
summed_human_events$EVTYPE <- tolower(summed_human_events$EVTYPE)
summed_human_events$EVTYPE_48_3 <- dictionary[amatch(summed_human_events$EVTYPE,dictionary,method="lcs", maxDist=20)]
table(summed_human_events$EVTYPE_48, summed_human_events$FATALITIES)
summed_human_events$EVTYPE[47] <- "extreme cold"
summed_human_events$EVTYPE[47]
summed_human_events$EVTYPE[67] <- "heat"
summed_human_events$EVTYPE[67]
summed_human_events$EVTYPE[217] <- "flooding"
summed_human_events$EVTYPE[217]