forked from ziyanfeng/help4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathData Processing.R
168 lines (117 loc) · 3.62 KB
/
Data Processing.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
168
# DS Hackathon
wd = getwd()
newdir = paste(wd, "/Desktop/DS Hackathon", sep = "")
setwd(newdir)
dataset = read.csv("fulldata_v6.csv", header = TRUE, sep = ",")
# dataset = dataset[, c(1, 3)]
# dataset = dataset[-nrow(dataset), ]
inc = dataset$Incident.Type
inc = as.character(inc)
cleansing = function(str) {
sep_pt = regexpr("-", str)[1]
str = strsplit(str, split = "")
splice_range = c(sep_pt: length(str[[1]]))
str[[1]] = str[[1]][-splice_range]
new_str = paste(str[[1]], collapse = "")
return(new_str)
}
new_inc = c(1:length(inc))
for (i in 1:length(inc)) {
new_inc[i] = cleansing(inc[i])
}
Incident.Type = as.factor(new_inc)
dataset$Incident.Type = Incident.Type
# write.csv(dataset, file = "fulldata_v5.csv")
# Categorized Creation Time
dataset$creationtime2 = as.character(dataset$creationtime2)
creationtime = as.POSIXlt(dataset$creationtime2, format="%m/%d/%y %H:%M")
Creation.Hour = creationtime$hour
assign_hour_range = function(hour) {
if (hour >=0 & hour <= 5) {
hour.range = 1
} else if (hour >= 6 & hour <= 11) {
hour.range = 2
} else if (hour >= 12 & hour <= 17) {
hour.range = 3
} else if (hour >= 18 & hour <= 23) {
hour.range = 4
}
return(hour.range)
}
Creation.Hour.Range = vector()
for (i in 1:length(Creation.Hour)) {
Creation.Hour.Range[i] = assign_hour_range(Creation.Hour[i])
}
Creation.Hour.Range = as.factor(Creation.Hour.Range)
dataset$Creation.Hour = Creation.Hour
dataset$Creation.Hour.Range = Creation.Hour.Range
# Get Date
creation_time = strsplit(dataset$creationtime2, split = " ")
Creation.Date = vector()
for (i in 1: length(dataset$creationtime2)) {
Creation.Date[i] = creation_time[[i]][1]
}
Creation.Date = as.Date(Creation.Date, format = "%m/%d/%y")
dataset$Creation.Date = Creation.Date
# weather data
nycweather = read.csv("nycweather.csv", header = FALSE, sep = ",")
nycweather = nycweather[-1, ]
names = c("Day", "JD", "Month", "State_id", "Year", "PRCP (in)", "TAVE (F)")
colnames(nycweather) = names
weather = nycweather[-1, ]
get_date_char = function(day, month, year) {
day = as.character(day)
month = as.character(month)
year = as.character(year)
combine = c(year, month, day)
date_char = paste(combine, collapse = "-")
#date = as.Date(date_char)
#return(date)
}
Date = vector()
Day = vector()
Month = vector()
Year = vector()
Day = weather$Day
Month = weather$Month
Year = weather$Year
for (i in 1:length(Day)) {
Date[i] = get_date_char(Day[i], Month[i], Year[i])
}
Date = as.Date(Date)
weather$Date = Date
weather_clean = weather[-c(1:5, 7)]
colnames(weather_clean) = c("Rain", "Date")
is_Rain = as.numeric(weather_clean$Rain)
for (i in 1:length(is_Rain)) {
if (is_Rain[i] == 2) {
is_Rain[i] = 0
} else {
is_Rain[i] = 1
}
}
weather_clean$Rain = is_Rain
cleandata = read.csv("cleandata.csv", header = TRUE, sep = ",")
cleandata = cleandata[, -c(1,2)]
# Add 2015 Weather Data
weather15 = read.csv("weather15.csv", header = FALSE, sep = ",")
Date15 = as.Date(weather15$V2, format = "%m/%d/%y")
weather15$V2 = Date15
colnames(weather15) = c("Rain", "Date")
new_weather = rbind(weather_clean, weather15)
# Merge weather Data
Weather_Date_all = new_weather$Date
Creation_Date_all = Creation.Date
Is.Rain = vector()
for (i in 1:length(Creation_Date_all)) {
for (j in 1:length(Weather_Date_all)) {
if (Weather_Date_all[j] %in% Creation_Date_all[i]) {
Is.Rain[i] = new_weather$Rain[j]
}
}
}
cleandata$Rain = Is.Rain
write.csv(cleandata, file = "cleandata_v2.csv")
finaldata = read.csv("fulldata_v11.csv", header = TRUE, sep = ",")
finaldata$Rain = Is.Rain
write.csv(finaldata, file = "fulldata_v12.csv")