R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
# working directory set in console
## Download the dataset into a new directory
fileurl <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
datadir <- "./data"
zipfile <- "projectdataset.bz2"
zipfilefullpath <- paste(datadir, "/", zipfile, sep="")

# create data dir if it doesn't exist yet
if(!file.exists(datadir)){dir.create(datadir)}

# download data file if it wasn't downloaded before
if(!file.exists(zipfilefullpath)){
  download.file(fileurl, destfile = zipfilefullpath)
}

d <- read.csv(zipfilefullpath)

Including Plots

You can also embed plots, for example:

require(reshape2)
## Loading required package: reshape2
## Warning: package 'reshape2' was built under R version 4.4.3
#transform data
d.aggr <- aggregate(cbind(FATALITIES, INJURIES) ~ EVTYPE, data = d, FUN = sum)
d.order <- d.aggr[order(d.aggr$INJURIES, decreasing = TRUE),]
d.head <- head(d.order, 8)
d.melt <- melt(d.head, id.var="EVTYPE")

#create plot
plot1 <- ggplot(d.melt, aes(x = EVTYPE, y = value, fill = variable)) +
  geom_bar(stat="identity") + 
  theme(axis.text.x = element_text(angle=45, hjust=1, vjust=.95)) +
  ylab("Number of people affected") + xlab("Event Type")
plot1

#show data used in a table
head(d.head, 8)
##             EVTYPE FATALITIES INJURIES
## 834        TORNADO       5633    91346
## 856      TSTM WIND        504     6957
## 170          FLOOD        470     6789
## 130 EXCESSIVE HEAT       1903     6525
## 464      LIGHTNING        816     5230
## 275           HEAT        937     2100
## 427      ICE STORM         89     1975
## 153    FLASH FLOOD        978     1777
#transform data
eco <- aggregate(cbind(PROPDMG, CROPDMG) ~ EVTYPE, data = d, FUN = sum)
eco.ordered <- eco[order(eco$PROPDMG, eco$CROPDMG, decreasing = TRUE),]
eco.dmg <- head(eco.ordered, 8)

#create plot
gp2 <- ggplot(eco.dmg, aes(x=EVTYPE, y=PROPDMG, fill=EVTYPE)) 
gp2 <- gp2 + geom_bar(stat="identity") 
gp2 <- gp2 + theme(axis.text.x = element_text(angle=45, hjust=1, size=8, color=2))
gp2 <- gp2 + geom_text(aes(x=eco.dmg$EVTYPE, y=eco.dmg$PROPDMG, angle=0, label=paste("$", floor(eco.dmg$PROPDMG), sep = ""), hjust=0.5, vjust=-0.5)) + theme(legend.position = "none")
gp2 <- gp2 + ylab("Property + Crop damage in dollars") + xlab("Event Type")
gp2
## Warning: Use of `eco.dmg$EVTYPE` is discouraged.
## ℹ Use `EVTYPE` instead.
## Warning: Use of `eco.dmg$PROPDMG` is discouraged.
## ℹ Use `PROPDMG` instead.
## Use of `eco.dmg$PROPDMG` is discouraged.
## ℹ Use `PROPDMG` instead.

#show data used in a table
head(eco.dmg, 8)
##                 EVTYPE   PROPDMG   CROPDMG
## 834            TORNADO 3212258.2 100018.52
## 153        FLASH FLOOD 1420124.6 179200.46
## 856          TSTM WIND 1335965.6 109202.60
## 170              FLOOD  899938.5 168037.88
## 760  THUNDERSTORM WIND  876844.2  66791.45
## 244               HAIL  688693.4 579596.28
## 464          LIGHTNING  603351.8   3580.61
## 786 THUNDERSTORM WINDS  446293.2  18684.93