
########################## 2012 year means

rm(list = ls())

library(plyr)

## Work
setwd("C:/Users/vovvie/Dropbox/master rescue/thesis files visbek/code")




##################-------------------- 

##### parties data import

parties = read.csv("parties_celebrations.csv", header=T, sep=";", fill=T)
parties = parties[c("Linn", "Kuupäev")] # extracting Location and Date columns
parties[parties==""] <- NA
parties <- na.omit(parties)
parties$Kuupäev = as.Date(parties$Kuupäev, "%d.%m.%Y")


##### rescue data
rescue = read.csv("rescue2012.csv", header=T, sep=";", fill=T)
rescue2012 <- rescue[!is.na(rescue$Väljakutse.nr),]
rescue2012 = subset(rescue2012, !duplicated(Väljakutse.nr))

rescue2012 = subset(rescue2012, select=c("Sündmuse.liik..SOS.", "VALLA_NIMI", "Registreerimise.aeg"))
names(rescue2012) <- c("Event", "Location", "Date")


##### choosing event type here
#events <- subset(rescue2012, grepl("liiklus", tolower(Event)))
#events <- subset(rescue2012, grepl("loodus", tolower(Event)))
"
events <- subset(rescue2012, grepl('insult', tolower(Event)) | grepl('pea', tolower(Event)) 
| grepl('inimene', tolower(Event)) | grepl('nõrkus', tolower(Event))
| grepl('valu', tolower(Event)) | grepl('kõht', tolower(Event))
| grepl('hingam', tolower(Event)) | grepl('kael', tolower(Event))
| grepl('teadvus', tolower(Event)) | grepl('vere', tolower(Event))
| grepl('jäse', tolower(Event)) | grepl('kõrgus', tolower(Event))
| grepl('iiveldus', tolower(Event)) | grepl('lamab', tolower(Event)) 
| grepl('torke', tolower(Event)) | grepl('rütm', tolower(Event)))
"
events <- subset(rescue2012, grepl("tulekahju", tolower(Event)))
#events <- subset(rescue2012, grepl('inimene', tolower(Event)))
#events <- rescue2012
print(paste("Number of rescue events", toString(nrow(events)), sep=" "))

## Dates 
events <- subset(events, select=c("Date")) # now subsetting only date
events$Day <- format(as.Date(events$Date, format="%d.%m.%Y"), "%Y.%m.%d")

event_days <- unique(count(events, "Day")) # finding unique days
names(event_days) <- c("Day", "Number.of.rescue.events")
print(paste("Number of days with rescue event", toString(nrow(event_days)), sep=" "))

##### parties

party = subset(parties, Kuupäev >= '2012-01-01' & Kuupäev <= '2012-12-31') # selecting only the year 2012
party <- subset(party, select=c("Kuupäev")) # selecting only Date column
print(paste("Number of public events (parties)", toString(nrow(party)), sep=" "))

party$Day <- format(as.Date(party$Kuupäev, format="%Y-%m-%d"), "%Y.%m.%d")

party_days <- unique(count(party, "Day")) # finding unique days
names(party_days) <- c("Day", "Number.of.public.events")
print(paste("Number of days with parties", toString(nrow(party_days)), sep=" "))

##### Merge weather and rescue events

result = merge(event_days, party_days, by = c("Day"), all=T) # include all observations

mean_with_parties = subset(result, !is.na(Number.of.public.events))
mean_with_parties[is.na(mean_with_parties[["Number.of.rescue.events"]]), "Number.of.rescue.events"] <- 0
mean_without_parties = subset(result, is.na(Number.of.public.events))

print(paste("With parties", toString(nrow(mean_with_parties)), sep=" "))
print(paste("Without parties", toString(nrow(mean_without_parties)), sep=" "))

##### calculating the mean

mean(mean_with_parties$Number.of.rescue.events)
mean(mean_without_parties$Number.of.rescue.events)







