-
Notifications
You must be signed in to change notification settings - Fork 0
/
MATCH(LEMO_WorkOrder, collisionDensity).R
66 lines (54 loc) · 2.49 KB
/
MATCH(LEMO_WorkOrder, collisionDensity).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
setwd("//ahmct-065/teams/PMRF/Amir")
library(data.table)
library(dplyr)
library(tidyr)
LEMO.df=fread(file = "./bin/Final Datasets/LEMO_WorkOrder+odom.csv", sep=",", header=TRUE)
LEMO.df[LEMO.df==""]=NA
LEMO.df=cbind(LEMO.df, LEMO_ID=seq.int(nrow(LEMO.df)))
temp_LEMO=LEMO.df[,c("LEMO_ID", "rID", "rSuffix", "beginCounty", "endCounty", "from.odom.R",
"to.odom.R", "from.odom.L", "to.odom.L")]
temp_LEMO$from.odom.L=as.numeric(temp_LEMO$from.odom.L)
temp_LEMO$from.odom.R=as.numeric(temp_LEMO$from.odom.R)
temp_LEMO$to.odom.L=as.numeric(temp_LEMO$to.odom.L)
temp_LEMO$to.odom.R=as.numeric(temp_LEMO$to.odom.R)
rm(LEMO.df)
collision_density=fread(file = "./bin/Final Datasets/CHP_density_2011-2012.csv", sep=",", header = TRUE)
collision_density[collision_density==""]=NA
collision_density=setDT(collision_density)[order(route, bin_start, bin_end), ,]
res=data.frame(matrix(NA, nrow=0, ncol=2))
colnames(res)=c("LEMO_ID", "density")
for (i in 1:length(temp_LEMO$LEMO_ID)){
route_id=temp_LEMO$rID[i]
if (!is.na(temp_LEMO$rSuffix[i])){
route_id=paste0(temp_LEMO$rID[i], temp_LEMO$rSuffix[i], sep="")
}
filtered_density=collision_density[which(collision_density$route==route_id),]
if (dim(filtered_density)[1]==0){
res=rbind(res, cbind.data.frame(LEMO_ID=temp_LEMO$LEMO_ID[i], density=0))
next
}
ind_begin=NA
ind_end=NA
density=NA
if (!is.na(temp_LEMO$from.odom.L[i]) & !is.na(temp_LEMO$to.odom.L[i])){
ind_begin=findInterval(temp_LEMO$from.odom.L[i], filtered_density$bin_start, rightmost.closed = TRUE)
ind_end=findInterval(temp_LEMO$to.odom.L[i], filtered_density$bin_start, rightmost.closed = TRUE)
if (!is.na(ind_begin) & !is.na(ind_end)){
density=filtered_density$freq[ind_begin:ind_end]
}
}
if (!is.na(temp_LEMO$from.odom.R[i]) & !is.na(temp_LEMO$to.odom.R[i])){
ind_begin=findInterval(temp_LEMO$from.odom.R[i], filtered_density$bin_start, rightmost.closed = TRUE)
ind_end=findInterval(temp_LEMO$to.odom.R[i], filtered_density$bin_start, rightmost.closed = TRUE)
if (!is.na(ind_begin) & !is.na(ind_end)){
density=c(density, filtered_density$freq[ind_begin:ind_end])
}
}
density=mean(density, na.rm = TRUE)
res=rbind(res, cbind.data.frame(LEMO_ID=temp_LEMO$LEMO_ID[i], density=density))
if(i%%100000==0){
print(i)
fwrite(res, file = "./bin/LEMO_ID.match.density_2011-2012.csv", sep=",", append = FALSE)
}
}
fwrite(res, file = "./bin/LEMO_ID.match.density_2011-2012.csv", sep=",", append = FALSE)