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:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
#Loading the libraries library(reshape2) library(reshape) library(ggplot2) library(plyr) library(dplyr)
setwd(“~/Desktop/Training/Data Science-Coursera/5 /Week 4/Peer-graded Assignment_Course Project 2”) storm_data <- read.csv(“StormData.csv.bz2”, header = T)
total_health <- aggregate(cbind(FATALITIES, INJURIES)~EVTYPE, data=storm_data, sum, na.rm=TRUE)
total_health_factor <- melt(total_health[order(-(total_health\(FATALITIES + total_health\)INJURIES)),][1:10,], id.vars = “EVTYPE”) names(total_health_factor) <- c(“EVTYPE”,“CAUSE”,“COUNT”)
storm_data <- transform(storm_data, PROPDMG = ifelse(PROPDMGEXP %in% “B”, PROPDMG10^9, ifelse(PROPDMGEXP %in% c(“m”, “M”), PROPDMG10^6, ifelse(PROPDMGEXP %in% c(“k”, “K”), PROPDMG10^3, ifelse(PROPDMGEXP %in% c(“h”, “H”), PROPDMG100, PROPDMG)))))
property_damage <- aggregate(PROPDMG~EVTYPE, data=storm_data, sum, na.rm = T) top_list <- property_damage[order(property_damage$PROPDMG, decreasing = T),][1:10,]
ggplot(total_health_factor, aes(x = reorder(EVTYPE, COUNT), y =
COUNT, fill = CAUSE)) +
geom_bar(stat = “identity”) + coord_flip() + scale_y_continuous(breaks =
seq(0, 100000, by = 2500)) + ylab(“Total injuries and fatalities”) +
xlab(“Event type”) + ggtitle(“The 10 most harmful events with respect to
population health”) + theme(axis.text.x = element_text(angle = 60, hjust
= 1))
ggplot(top_list, aes(x = reorder(EVTYPE, PROPDMG/10e9), y = PROPDMG/10e9)) + geom_bar(stat = “identity”, fill = “darkturquoise”) + coord_flip() + ylab(“Property damage in billions of dollars”) + xlab(“Event type”) + ggtitle(“The 10 most harmful events with respect to property damage”)
url <- “https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2” download.file(url, “repdata%2Fdata%2FStormData.csv.bz2”) dataset <- read.csv(“repdata%2Fdata%2FStormData.csv.bz2”, header = TRUE, sep = “,”) dataset<-subset(dataset,select=c(“EVTYPE”, “FATALITIES”, “INJURIES”,“PROPDMG”,“PROPDMGEXP”,“CROPDMG”,“CROPDMGEXP”)) ## Process Data using values in exponential columns to calculate actual Property and Crop Damage. dataset\(PROPDMGEXP=case_when( dataset\)PROPDMGEXP %in% c (‘K’,‘k’)~3, dataset\(PROPDMGEXP %in% c ('M','m')~6, dataset\)PROPDMGEXP %in% c (‘B’,‘b’)~9, dataset\(PROPDMGEXP %in% c ('H','h')~2, !is.na(as.numeric(dataset\)PROPDMGEXP))~as.numeric(dataset\(PROPDMGEXP), dataset\)PROPDMGEXP %in% c(““,”-“,”?“,”+“)~0, TRUE~0 )
dataset\(CROPDMGEXP=case_when( dataset\)CROPDMGEXP %in% c (‘K’,‘k’)~3, dataset\(CROPDMGEXP %in% c ('M','m')~6, dataset\)CROPDMGEXP %in% c (‘B’,‘b’)~9, dataset\(CROPDMGEXP %in% c ('H','h')~2, !is.na(as.numeric(dataset\)CROPDMGEXP))~as.numeric(dataset\(CROPDMGEXP), dataset\)CROPDMGEXP %in% c(““,”-“,”?“,”+“)~0, TRUE~0 ) dataset\(CROPDMG<-dataset\)CROPDMG*10**dataset\(CROPDMGEXP dataset\)PROPDMG<-dataset\(PROPDMG*10**dataset\)PROPDMGEXP
datasetByHealthHazard<- dataset %>% group_by(EVTYPE) %>% summarise(FATALITIES=sum(FATALITIES),INJURIES=sum(INJURIES)) datasetByFatalities<-datasetByHealthHazard[order(datasetByHealthHazard\(FATALITIES,decreasing=TRUE),] ##datasetByFatalities<-head(datasetByFatalities,5) datasetByInjuries<-datasetByHealthHazard[order(datasetByHealthHazard\)INJURIES,decreasing=TRUE),] ##datasetByInjuries<-head(datasetByInjuries,5)
datasetByEcoDamage<- dataset%>% group_by(EVTYPE) %>% summarise(CROPDMG=sum(CROPDMG),PROPDMG=sum(PROPDMG)) head(datasetByEcoDamage)
#################################################3
p1 <- ggplot(data=datasetByFatalities, aes(x=reorder(EVTYPE, FATALITIES), y=FATALITIES, fill=EVTYPE))+ geom_bar(stat=“identity”) + coord_flip() + ylab(“Total number of FATALITIES”) + xlab(“Event type”) p2 <- ggplot(data=datasetByInjuries, aes(x=reorder(EVTYPE, INJURIES), y=INJURIES, fill=EVTYPE)) + geom_bar(stat=“identity”) + coord_flip() + ylab(“Total number of INJURIES”) + xlab(“Event type”)
grid.arrange(p1, p2,nrow=2)
p3 <- ggplot(data=datasetByPropDamage, aes(x=reorder(EVTYPE, PROPDMG), y=PROPDMG, fill=EVTYPE))+ geom_bar(stat=“identity”) + coord_flip() + ylab(“Total Value of Property Damage Dollars.”) + xlab(“Event type”) p4 <- ggplot(data=datasetByCropDamage, aes(x=reorder(EVTYPE, CROPDMG), y=CROPDMG, fill=EVTYPE)) + geom_bar(stat=“identity”) + coord_flip() + ylab(“Total Value of Crop Damage Dollars.”) + xlab(“Event type”) grid.arrange(p3, p4,nrow=2, top=“Economic Impact of Weather”)