
########################## 2012 weekday means

rm(list = ls())

library(plyr)

Sys.setlocale("LC_TIME", "C")

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




##################-------------------- Pidu ettevalmistus (EI MUUDA!)

##### 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

## Dates 
events$Weekday = format(as.Date(events$Date, format="%d.%m.%Y"), "%A")

##### Loop üle kõigi nädalapäevade
for (wday in c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")) {
  print(wday)
  
  # similarly to months first we subset each month and find number of rescue events
  rescue_weekday = subset(events, Weekday == wday)
  
  rescue_weekday <- subset(rescue_weekday, select=c("Date"))
  rescue_weekday$Date <- format(as.Date(rescue_weekday$Date, format="%d.%m.%Y"), "%Y.%m.%d")
  "
  # we can also remove biggest estonian holidays, so that they do not affect our results
  rescue_weekday = subset(rescue_weekday, Date != '2012.12.31' & Date != '2012.02.24'
  & Date != '2012.03.08' & Date != '2012.06.23'
  & Date != '2012.06.24' & Date != '2012.12.24'
  & Date != '2012.12.25' & Date != '2012.04.14'
  & Date != '2012.05.01' & Date != '2012.12.26'
  & Date != '2012.09.01' & Date != '2012.01.01')
  "
  print(paste('Number of rescue events', toString(nrow(rescue_weekday)), sep=' '))
  
  rescue_weekday <- unique(count(rescue_weekday, "Date")) 
  print(paste("Number of days with rescue event", toString(nrow(rescue_weekday)), sep=" "))
  
  names(rescue_weekday) <- c("Weekday", "Number.of.rescue.events")
  
  ##### parties
  
  # here we find number of parties (also for each weekday, in a loop)
  party = subset(parties, Kuupäev >= '2012-01-01' & Kuupäev <= '2012-12-31')
  
  party$Weekday = format(as.Date(party$Kuupäev, format="%Y-%m-%d"), "%A")
  party_weekday = subset(party, Weekday == wday)
  
  party_weekday <- subset(party_weekday, select=c("Kuupäev"))
  party_weekday$Kuupäev <- format(as.Date(party_weekday$Kuupäev, format="%Y-%m-%d"), "%Y.%m.%d")
  "
  party_weekday = subset(party_weekday, Kuupäev != '2012.12.31' & Kuupäev != '2012.02.24'
  & Kuupäev != '2012.03.08' & Kuupäev != '2012.06.23'
  & Kuupäev != '2012.06.24' & Kuupäev != '2012.12.24'
  & Kuupäev != '2012.12.25' & Kuupäev != '2012.04.14'
  & Kuupäev != '2012.05.01' & Kuupäev != '2012.12.26'
  & Kuupäev != '2012.09.01' & Kuupäev != '2012.01.01')
  "
  print(paste("Number of public events", toString(nrow(party_weekday)), sep=" "))
  party_weekday <- unique(count(party_weekday, "Kuupäev")) 
  
  names(party_weekday) <- c("Weekday", "Number.of.public.events")
  
  ##### Merge
  
  # as a result we find mean of numbers of rescue events on days with and without parties
  result = merge(rescue_weekday, party_weekday, by = c("Weekday"), all=T) 
  
  weekday_mean_with_parties = subset(result, !is.na(Number.of.public.events))
  weekday_mean_with_parties[is.na(weekday_mean_with_parties[["Number.of.rescue.events"]]), "Number.of.rescue.events"] <- 0
  print(paste("With parties", toString(nrow(weekday_mean_with_parties)), sep=" "))
  
  weekday_mean_without_parties = subset(result, is.na(Number.of.public.events)) 
  print(paste("Without parties", toString(nrow(weekday_mean_without_parties)), sep=" "))
  
  ##### Average
  print(paste("Mean 1 ", toString(mean(weekday_mean_with_parties$Number.of.rescue.events)), sep=" "))
  print(paste("Mean 2 ", toString(mean(weekday_mean_without_parties$Number.of.rescue.events)), sep=" "))
  print(NULL)
}

# the final sequence are 7 in length, as there are 7 weekdays in a week


