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:
if(!file.exists("./repdata_data_StormData.csv.bz2")){
file.create("./repdata_data_StormData.csv.bz2")
download.file("https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2",
"./repdata_data_StormData.csv.bz2")
}
## Calculate total human casualties as sum of fatalities and injuries
storm<-read.csv("./repdata_data_StormData.csv.bz2", header=T)
storm$FandI<-storm$FATALITIES+storm$INJURIES
## Data Conversion: Factor data to character
storm$PROPDMGEXP<-as.character(storm$PROPDMGEXP)
storm$CROPDMGEXP<-as.character(storm$CROPDMGEXP)
## Property and crop damage units and respective column numbers
propcol<-grep("PROPDMGEXP",colnames(storm))
cropcol<-grep("CROPDMGEXP",colnames(storm))
## Calculation Support: Convert property and crop damage units
storm[storm$PROPDMGEXP=="",propcol]<-"0"
storm[storm$PROPDMGEXP=="-",propcol]<-"0"
storm[storm$PROPDMGEXP=="?",propcol]<-"0"
storm[storm$PROPDMGEXP=="+",propcol]<-"0"
storm[storm$PROPDMGEXP=="1",propcol]<-"0"
storm[storm$PROPDMGEXP=="2",propcol]<-"0"
storm[storm$PROPDMGEXP=="3",propcol]<-"0"
storm[storm$PROPDMGEXP=="4",propcol]<-"0"
storm[storm$PROPDMGEXP=="5",propcol]<-"0"
storm[storm$PROPDMGEXP=="6",propcol]<-"0"
storm[storm$PROPDMGEXP=="7",propcol]<-"0"
storm[storm$PROPDMGEXP=="8",propcol]<-"0"
storm[storm$PROPDMGEXP=="h",propcol]<-"100"
storm[storm$PROPDMGEXP=="H",propcol]<-"100"
storm[storm$PROPDMGEXP=="K",propcol]<-"1000"
storm[storm$PROPDMGEXP=="m",propcol]<-"1000000"
storm[storm$PROPDMGEXP=="M",propcol]<-"1000000"
storm[storm$PROPDMGEXP=="B",propcol]<-"1000000000"
storm[storm$CROPDMGEXP=="",cropcol]<-"0"
storm[storm$CROPDMGEXP=="?",cropcol]<-"0"
storm[storm$CROPDMGEXP=="2",cropcol]<-"0"
storm[storm$CROPDMGEXP=="k",cropcol]<-"1000"
storm[storm$CROPDMGEXP=="K",cropcol]<-"1000"
storm[storm$CROPDMGEXP=="m",cropcol]<-"1000000"
storm[storm$CROPDMGEXP=="M",cropcol]<-"1000000"
storm[storm$CROPDMGEXP=="B",cropcol]<-"1000000000"
## Data Convertion: Property and crop damage units to numeric
storm$PROPDMGEXP<-as.numeric(storm$PROPDMGEXP)
storm$CROPDMGEXP<-as.numeric(storm$CROPDMGEXP)
## Calculation: To find total property and crop damage losses
storm$PropLoss<-storm$PROPDMG*storm$PROPDMGEXP
storm$CropLoss<-storm$CROPDMG*storm$CROPDMGEXP
## Calculation: To find total economic losses
storm$EconomicLoss<-storm$PropLoss+storm$CropLoss
## 1. Identify column numbers for human loss calculations
col1<-grep("EVTYPE",colnames(storm))
col2<-grep("FandI",colnames(storm))
## 2. Creation of small data set and omitting NA values
HumanLoss<-storm[,c(col1,col2)]
HumanLoss<-na.omit(HumanLoss)
## 3. Identification of relevant column numbers for economic loss calculations
col1<-grep("EVTYPE",colnames(storm))
col2<-grep("EconomicLoss",colnames(storm))
## 4. Creation of small data set and omit NA Values
EcoLoss<-storm[,c(col1,col2)]
EcoLoss<-na.omit(EcoLoss)
## Aggregation of total human losses over all events
HumanLossAgg<-aggregate(FandI~EVTYPE,HumanLoss,sum,na.rm=TRUE)
## Sort human losses data in decreasing order of losses
HumanLossAgg<-HumanLossAgg[order(HumanLossAgg$FandI,decreasing = TRUE),]
## Create small dataset for plotting only top 3 events
HumanLossAggShort<-HumanLossAgg[1:3,]
## Aggregation of total economic losses over all events
EcoLossAgg<-aggregate(EconomicLoss~EVTYPE,EcoLoss,sum,na.rm=TRUE)
## Sort economic losses data in decreasing order of losses
EcoLossAgg<-EcoLossAgg[order(EcoLossAgg$EconomicLoss,decreasing = TRUE),]
## Create small dataset for plotting only top 3 events
EcoLossAggShort<-EcoLossAgg[1:3,]
library(ggplot2)
You can also embed plots, for example:
## Plot bars for top 3 events and their losses
hplot<-ggplot(data=EcoLossAggShort,aes(x=EVTYPE,y=EconomicLoss))
hplot+geom_bar(stat="identity")+xlab("Storm Event")+ylab("Economic Loss ($)")+labs(title="Top 3 Storm Events to Cause Max Economic Losses")
Note that the
echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.