-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask1.R
167 lines (120 loc) · 5.49 KB
/
task1.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
# importing libraries
library (tabulizer)
library (dplyr)
library (tidyr)
library (ggplot2)
library (stringr)
# extract all tables from the file
tab = extract_tables (file = "~/ankit/Github/Data-Analytics/datasets/task1/CRS-2016.pdf")
# Display table 6
tab [[6]]
# Display data for years 2011 to 16
tab [[6]][17:22, ]
# Put this in data frame
df = as.data.frame (tab [[6]] [17:22, ])
# Print data frame
df
# Tidy the data by seperating V2, V3 and V4 into seperate columns
df = separate(df, V2, into = c("2", "3", "4"), sep = ' ')
df = separate(df, V3, into = c("5", "6"), sep = ' ')
df = separate(df, V4, into = c("7", "8"), sep = ' ')
# Rename columns
colnames (df) [colnames(df) == "V1"] <- "year"
colnames (df) [colnames(df) == "2"] <- "live.births"
colnames (df) [colnames(df) == "3"] <- "still.birth"
colnames (df) [colnames(df) == "4"] <- "deaths"
colnames (df) [colnames(df) == "5"] <- "crs.births"
colnames (df) [colnames(df) == "6"] <- "crs.deaths"
colnames (df) [colnames(df) == "7"] <- "srs.births"
colnames (df) [colnames(df) == "8"] <- "srs.deaths"
# Print the tidy data frame
df
vital = df [, 1:4]
vital$year = vital$year
vital$live.births = as.numeric(vital$live.births) /1000
vital$still.birth = as.numeric(vital$still.birth) /10
vital$deaths = as.numeric(vital$deaths) / 400
# Changes for plot facilitation
y = c()
for (year in vital$year) {
y = c(y, rep (c(year), 3))
}
class = rep (c("live.births", "still.birth", "deaths"), 6)
val = c()
for (i in 1:length (vital [,1])) {
val = c(val, c(vital$live.births[i], vital$still.birth[i], vital$deaths[i]))
}
data = data.frame (y, class, val)
# Plot 1: Vital statistics
# Death: 1 unit is 400 deaths
# Live birth: 1 unit is 1000 live births
# Still birth: 1 unit is 10 still births
ggplot(data, aes(fill=class, y=val, x=factor (y))) + geom_bar(position="dodge", stat="identity") + xlab ("Years") + ylab ("Value") + ggtitle ("Vital events: Birth, Still Birth and Deaths registered in the year 2011-16") + scale_fill_discrete (labels=c("Deaths (x400)", "Live Birth (x1000)", "Still Birth (x10)"))
###############################################################################
# District wise registered birth and deaths
dist = tab [[10]]
dist
# Clean-up the data
dist = dist [9:38,]
dist
dist = as.data.frame (dist)
dist
dst = str_extract(dist$V2, '[^[:digit:]]+')
val = str_remove_all (dist$V2, "[[A-Z]\\(\\)]")
val = str_remove (val, "[:space:]+")
dist = data.frame (District = dst, v = val)
dist = separate(dist, v, into = c("Birth", "Birth.rate", "Death", "Death.rate", "Infant.death", "Still.Birth", "Still.Birth.rate"), sep = '[:blank:]+')
dist$Birth = as.numeric (dist$Birth)
dist$Birth.rate = as.numeric (dist$Birth.rate)
dist$Death = as.numeric (dist$Death)
dist$Death.rate = as.numeric (dist$Death.rate)
dist$Infant.death = as.numeric (dist$Infant.death)
dist$Still.Birth = as.numeric (dist$Still.Birth)
dist$Still.Birth.rate = as.numeric (dist$Still.Birth.rate)
# cleaned up data frame
dist
rates = data.frame (District = dist$District, Birth.rate = dist$Birth.rate, Death.rate = dist$Death.rate, Still.Birth.rate = dist$Still.Birth.rate)
rates
# rate data for plotting
di = c()
for (dis in rates$District) {
di = c(di, rep (c(dis), 3))
}
class = rep (c("Birth.rate", "Death.rate", "Still.Birth.rate"), 30)
val = c()
for (i in 1:length (vital [,1])) {
val = c(val, c(rates$Birth.rate[i] / 2, rates$Death.rate[i], rates$Still.Birth.rate[i]))
}
dis_data = data.frame (di, class, val)
# plot
# Birth rate: multiply the unit by 2 to get actual rate
ggplot(dis_data, aes(fill=class, y=val, x=factor (di))) + geom_bar(position="dodge", stat="identity") + xlab ("Districts") + ylab ("rates") + ggtitle ("Birth, Death and Still Birth rates in Urban areas of districts\nKarnataka 2016") + scale_fill_discrete (labels=c("Birth rate (x2)", "Death rate", "Still Birth rate")) + coord_flip()
# Boxplot for different rates accross districts 2016
boxplot (rates$Birth.rate, rates$Death.rate, rates$Still.Birth.rate, names = c("Birth rate", "Death rate", "Still Birth rate"), ylab = "Rates", main = "Rate boxplot (No outliers for any rate\ndepicting no differnece in rate across Urban\nareas in all districts)")
# Finding outliers
br_out = boxplot (dist$Still.Birth.rate)$out
br_out
which (br_out %in% dist$Birth.rate)
# No outliers in the plots, therefore the rates are similar accross all districts
# Pie charts showing the contribution of each district in the total birth and death accross the state
pie (dist$Birth, labels = dist$District, col=rainbow(length(dist$District)), main = "Birth contribution accross Districts in Urban areas-2016", radius = 0.9, cex = 0.7, font = 2, init.angle = 46)
pie (dist$Death, labels = dist$District, col=rainbow(length(dist$District)), main = "Death contribution accross Districts in Urban areas-2016", radius = 0.9, cex = 0.7, font = 2, init.angle = 40)
####################
# Basic Statistics #
####################
# District with minimum birth rate 2016
rates [which (rates$Birth.rate == min (rates$Birth.rate)),c(1,2)]
# District with maximum birth rate 2016
rates [which (rates$Birth.rate == max (rates$Birth.rate)),c(1,2)]
# Mean of birth rate 2016
print (mean (rates$Birth.rate))
# Mean birth accross districts 2016
print (as.integer (mean (dist$Birth)))
# Median of birth rate accross districts 2016
print (median (rates$Birth.rate))
# varience of birth rate accross districts
print (var (rates$Birth.rate))
# Standard deviation of birth rate accross districts 2016
print (sd (rates$Birth.rate))
# Total summary of rates accross districts in 2016
summary (rates)