forked from MeetDarkPow/Rainfall-Data-Analysis-India-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRainfall_Data_Analysis_-_Report.Rmd
179 lines (159 loc) · 7.94 KB
/
Rainfall_Data_Analysis_-_Report.Rmd
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
---
title: "Rainfall Data Analysis"
author: "Meet Bhatnagar and Digvijay Singh"
date: "07/06/2019"
output: pdf_document
---
# Analysis of 111 years of Rainfall Data from 596 districts of India.
```{r,echo=FALSE,warning=FALSE,error=FALSE,message=FALSE,cache=TRUE}
#---------------------------------------------------------------------------------------------------------------------------------------------------------
# Reading in Data as data frame, converting into Time series.
#---------------------------------------------------------------------------------------------------------------------------------------------------------
# Loading library "readxl" for reading in .xlsx file via R.
# Loading library "openxlsx" for writing in .xlsx file via R.
library(readxl)
library(openxlsx)
# Data from file is stored in the data frame named as 'Intermediate'.
# "read_excel" function of "readxl" library is used.
Intermediate<-read_excel("Rainfall_Data_(111_years).xlsx")
# Removing NA values from 'Intermediate'.
Intermediate<-na.omit(Intermediate)
# Finding names of each district seperately using "unique" function.
Unique<-unique(Intermediate$DIST)
# "which" function is used to find the row number for which certain value is present in the data frame.
# This function can be used to create subset of data for each district.
# In order to view an example for showing the working of "which" function, remove hash from the following snippet.
# Intermediate[which(Intermediate$DIST==Unique[1])[1]:which(Intermediate$DIST==Unique[1])[length(which(Intermediate$DIST==Unique[1]))],]
# Creating a folder for storing all .xlsx files having data for each district seperately.
dir.create("Intermediate")
# Creating loop for writing an excel file with a particular district data.
for(i in 1:length(Unique))
{
write.xlsx(Intermediate[which(Intermediate$DIST==Unique[i])[1]:which(Intermediate$DIST==Unique[i])[length(which(Intermediate$DIST==Unique[i]))],-1:-3],
file=paste0("Intermediate/Intermediate_",i,".xlsx"),
sheetName = Unique[i], col.names = TRUE, row.names = TRUE, append = TRUE)
}
# Reading each file and changing its rownames with years.
# Removing all "WC" data.
# Storing each district's data in a separate data frame having the name of that particular district.
for(j in 1:length(Unique))
{
Data<-read_excel(paste0("Intermediate/Intermediate_",j,".xlsx"))
Data<-as.data.frame(Data)
row.names(Data)<-Intermediate$Year[1:length(row.names(Data))]
Data<-Data[,-1]
assign(Unique[j],Data)
}
# Deleting "Intermediate" directory.
unlink("Intermediate",recursive=T)
```
## Analysis of Annual, Monthwise Rainfall Data and Deviation of Annual rainfall from Normal rainfall for each district. And analysis of Above and Below Normal Rainfall values for each district per year.
```{r,echo=FALSE,warning=FALSE,error=FALSE,message=FALSE,cache=TRUE}
#---------------------------------------------------------------------------------------------------------------------------------------------------------
# Plotting of -
# 1) Monthwise data
# 2) Annual data
# 3) 30 year moving average estimation
# 4) deviation of actual rainfall from 30 year moving average using Annual rainfall data
# for each district .
#---------------------------------------------------------------------------------------------------------------------------------------------------------
# Creating a folder for storing all analysis charts for each district seperately.
dir.create("Analysis")
for(k in 1:length(Unique))
{
# Creating a folder for each district.
dir.create(paste0("Analysis/",Unique[k]))
}
# Converting each data frame containing district rainfall data into time series and plotting its graph.
library(pracma)
for(l in 1:length(Unique))
{
Data<-eval(parse(text=paste0("`",Unique[l],"`")))
# For plotting monthwise.
Month<-Data
Month<-(as.vector(t(as.matrix(Month[,-13:-18]))))
Month<-ts(Month,frequency=12,start=Intermediate$Year[1],end=Intermediate$Year[length(Intermediate$Year)])
# For plotting Annual Rainfall.
Annual<-Data
Annual<-(as.vector(t(as.matrix(Annual[,13]))))
Annual<-ts(Annual,frequency=1,start=Intermediate$Year[1],end=Intermediate$Year[length(Intermediate$Year)])
#Plotting graphs.
plot(Month,main=paste0(Unique[l]," - Monthwise"),ylab="Rainfall in mm")
plot(Annual,main=paste0(Unique[l]," - Annual"),ylab="Rainfall in mm")
plot(movavg(Annual,n=30),type="l",ylab="Rainfall in mm",main=paste0("Moving Average for 30 years - ",Unique[l]),xlab="Period (Number of years from 1901)")
plot(Annual-movavg(Annual,n=30),type="l",ylab="Rainfall in mm",main=paste0("Deviation of Actual Rainfall from 30 years Moving Average - ",Unique[l]),xlab="Period (Number of years from 1901)")
#----------------------------------------------------------------------------
# Again creating dataframes each having a seperate district data with the same name as the respective district.
Data<-eval(parse(text=paste0("`",Unique[l],"`")))[13]
# Finding normal rainfall value.
Normal<-sum(Data)/length(Data$WC13)
Data$Frequency<-c(0)
for(p in 1:length(rownames(Data)))
{
if(Data$WC13[p]>=(Normal*1.1))
{
Data$Frequency[p]=1
}
else if(Data$WC13[p]<=(Normal*0.9))
{
Data$Frequency[p]=-1
}
}
plot(Data$WC13,type="l",main=paste0("Above & Below Normal Rainfall and its Frequency - ",Unique[l]),ylab="Rainfall in mm",xlab="Period (Number of years past 1901)")
points(Data$WC13,col=ifelse(Data$Frequency==1,"Red",ifelse(Data$Frequency==0,"Black","Blue")),pch=20)
abline(h=Normal*1.1,col="Red")
abline(h=Normal*0.9,col="Blue")
legend('bottomleft',horiz=TRUE,c(paste0("Above Normal Frequency - ",sum(Data$Frequency==1,na.rm=TRUE)," years"),
paste0("Below Normal Frequency - ",sum(Data$Frequency==-1,na.rm=TRUE)," years")),cex=.7,
fill=c("red","blue"),bty='n')
}
```
## Analysis of percentage of districts having Above and Below normal rainfall for each year.
```{r,echo=FALSE,warning=FALSE,error=FALSE,message=FALSE,cache=TRUE}
# Creating vector for storing all normal rainfall values for each district.
Normal_Rainfall<-vector(mode='numeric',length=length(Unique))
for(q in 1:length(Unique))
{
Temp<-eval(parse(text=paste0("`",Unique[q],"`")))[13]
Normal_Rainfall[q]=sum(Temp$WC13)/length(Temp$WC13)
}
# Converting into a data frame.
Normal_Rainfall<-as.data.frame(Normal_Rainfall)
rownames(Normal_Rainfall)<-Unique
# Adding frequency of districts having above and below normal rainfall for all years.
for(r in 1:length(rownames(Data)))
{
Frequency<-vector(mode='numeric',length=length(Unique))
for(s in 1:length(Unique))
{
if(((eval(parse(text=paste0("`",Unique[s],"`")))$WC13[r]))>=(Normal_Rainfall$Normal_Rainfall[s]*1.1))
{
Frequency[s]=1
}
else if(((eval(parse(text=paste0("`",Unique[s],"`")))$WC13[r]))<=(Normal_Rainfall$Normal_Rainfall[s]*0.9))
{
Frequency[s]=-1
}
}
Normal_Rainfall[paste0(rownames(Data)[r])]<-Frequency
}
Normal_Rainfall<-Normal_Rainfall[,-1]
Above_Normal<-vector(mode='numeric',length=length(rownames(Data)))
Below_Normal<-vector(mode='numeric',length=length(rownames(Data)))
for(t in 1:length(rownames(Data)))
{
Above_Normal[t]=(sum(Normal_Rainfall[,t]==1,na.rm=T)/length(Unique))*100
Below_Normal[t]=(sum(Normal_Rainfall[,t]==-1,na.rm=T)/length(Unique))*100
}
Above_Normal<-as.data.frame(Above_Normal)
rownames(Above_Normal)<-rownames(Data)
Below_Normal<-as.data.frame(Below_Normal)
rownames(Below_Normal)<-rownames(Data)
# Saving plot on percentage of districts having above and below normal rainfall for each year.
plot(ts(Above_Normal,start=as.numeric(rownames(Above_Normal)[1]),
end=as.numeric(rownames(Above_Normal)[length(rownames(Above_Normal))])),
main="Percentage of districts having above normal rainfall",ylab="Percentage of Districts",xlab="Years",col="Red")
plot(ts(Below_Normal,start=as.numeric(rownames(Below_Normal)[1]),
end=as.numeric(rownames(Below_Normal)[length(rownames(Below_Normal))])),
main="Percentage of districts having below normal rainfall",ylab="Percentage of Districts",xlab="Years",col="Blue")
```