-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcc.R
281 lines (175 loc) · 6.74 KB
/
tcc.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
##TRATAMENTO DE DADOS E FORMAÇÃO DA REDE
library(tidyr)
library(tidyverse)
library(dplyr)
library(lubridate)
library(hms)
#leitura dos bancos de dados
d1 <- read.csv2("~/Downloads/movimentacao_sobe_desce_29-11-2018-001.csv")
d2 <- read.csv2("C:/Users/allan/Downloads/movimentacao_sobe_desce_29-11-2018-002.csv")
d3<- read.csv2("~/Downloads/movimentacao_sobe_desce_29-11-2018-003.csv")
d4<- read.csv2("~/Downloads/movimentacao_sobe_desce_29-11-2018-004.csv")
dataset <- rbind(d1, d2, d3, d4) #união de todos
dataset$Data.Pesquisa <- dmy(dataset$Data.Pesquisa) ##para entender como data
##vendo quais datas temos
datas <- unique(dataset[c("Data.Pesquisa")])
arrange(datas, desc(datas))
dataset <- dataset %>%
dplyr::mutate(Hora.Viagem = format(strptime(lubridate::parse_date_time(Hora.Viagem, 'H:M'),
"%Y-%m-%d %H:%M:%S"),'%H:%M'))
dataset$DT <- with(dataset, ymd(Data.Pesquisa) + hm(Hora.Viagem))
table(is.na(dataset$Hora.Viagem)) ##37 NA
########testando só em d1
d1$Data.Pesquisa <- dmy(d1$Data.Pesquisa)
d1 <- d1 %>%
dplyr::mutate(Time = format(strptime(lubridate::parse_date_time(Hora.Viagem, 'H:M'),
"%Y-%m-%d %H:%M:%S"),'%H:%M'))
d1$DT <- with(d1, ymd(Data.Pesquisa) + hm(Time))
d2$Data.Pesquisa <- dmy(d2$Data.Pesquisa)
d2$Time<- format(strptime(hm(d2$Hora.Viagem),'%H:%M'))
d2 <- d2 %>%
dplyr::mutate(Time = format(strptime(hm(d2$Hora.Viagem)), '%H:%M'))
d2 <- d2 %>%
dplyr::mutate(Time = format(strptime(lubridate::parse_date_time(Hora.Viagem, 'H:M'),
"%Y-%m-%d %H:%M:%S"),'%H:%M'))
d2$DT <- with(d2, ymd(Data.Pesquisa) + hm(Time))
##Quero olhar só terca no d1
d1$Data.Pesquisa <- dmy(d1$Data.Pesquisa)
d1 <- d1 %>%
dplyr::mutate(Time = format(strptime(lubridate::parse_date_time(Hora.Viagem, 'H:M'),
"%Y-%m-%d %H:%M:%S"),'%H:%M'))
d1$DT <- with(d1, ymd(Data.Pesquisa) + hm(Time))
teste <- subset(d1, d1$Dia.Semana == "Terca ") ##não usar coluna de dias semana T.T
test <- teste %>% filter(Data.Pesquisa == "2009-02-10")
##Olhando tb tudo que há nesse dia
test2 <- dataset %>% filter(Data.Pesquisa == "2009-02-10")
##Reajustando coordenadas incorretas
pos=which(dataset$Endereco.do.PED=="AVE PADRE JOSE MAURICIO 46 ")
dataset$Latitude[pos]=-19.94964
dataset$Longitude[pos]=-43.99045
pos=which(dataset$Endereco.do.PED=="RUA FORMIGA 294 ")
dataset$Latitude[pos]=-19.90432
dataset$Longitude[pos]=-43.94531
pos=which(dataset$Endereco.do.PED=="AVE BERNARDO MONTEIRO 390 ")
dataset$Latitude[pos]=-19.92130
dataset$Longitude[pos]=-43.92722
#Agora aqui uma coisa interessante - dado o dia temos o dataset do dia.
load("dataset completo.Rda")
pos=which(dataset$Endereco.do.PED=="AVE PADRE JOSE MAURICIO 46 ")
dataset$Latitude[pos]=-19.94964
dataset$Longitude[pos]=-43.99045
pos=which(dataset$Endereco.do.PED=="RUA FORMIGA 294 ")
dataset$Latitude[pos]=-19.90432
dataset$Longitude[pos]=-43.94531
pos=which(dataset$Endereco.do.PED=="AVE BERNARDO MONTEIRO 390 ")
dataset$Latitude[pos]=-19.92130
dataset$Longitude[pos]=-43.92722
dias=union(as.character(dataset$Dia.Semana),as.character(dataset$Dia.Semana))
rotas=intersect(dataset$Rota,dataset$Rota)
df=subset(dataset,Dia.Semana==dias[3])
DF=df
df=subset(dataset,Rota==rotas[1])
linha=intersect(as.character(df$Linha.SubLinha),as.character(df$Linha.SubLinha))
d=data.frame(de="a",para="b")
peso=c()
for(onibus in linha){
df1=subset(df,Linha.SubLinha==onibus)
periodo=nrow(df1)/max(df1$Sequencia.do.PED)
v=df1$Endereco.do.PED[1:max(df1$Sequencia.do.PED)]
d=rbind(d,data.frame(de=v[1:(length(v)-1)],para=v[2:length(v)]))
}
d=d[2:nrow(d),]
d=na.omit(d)
g=graph.data.frame(d,directed=TRUE)
g=simplify(g)
elos=get.edgelist(g)
peso=c()
for(rua in elos[,1]){
peso=c(peso,sum(df$Saldo[which(df$Endereco.do.PED==rua)]))
}
E(g)$weight=peso
g1=g
df=subset(dataset,Rota==rotas[2])
linha=intersect(as.character(df$Linha.SubLinha),as.character(df$Linha.SubLinha))
d=data.frame(de="a",para="b")
peso=c()
for(onibus in linha){
df1=subset(df,Linha.SubLinha==onibus)
periodo=nrow(df1)/max(df1$Sequencia.do.PED)
v=df1$Endereco.do.PED[1:max(df1$Sequencia.do.PED)]
d=rbind(d,data.frame(de=v[1:(length(v)-1)],para=v[2:length(v)]))
}
d=d[2:nrow(d),]
d=na.omit(d)
g=graph.data.frame(d,directed=TRUE)
g=simplify(g)
elos=get.edgelist(g)
peso=c()
for(rua in elos[,1]){
peso=c(peso,sum(df$Saldo[which(df$Endereco.do.PED==rua)]))
}
E(g)$weight=peso
g2=g
df=subset(dataset,Rota==rotas[3])
linha=intersect(as.character(df$Linha.SubLinha),as.character(df$Linha.SubLinha))
d=data.frame(de="a",para="b")
peso=c()
for(onibus in linha){
df1=subset(df,Linha.SubLinha==onibus)
periodo=nrow(df1)/max(df1$Sequencia.do.PED)
v=df1$Endereco.do.PED[1:max(df1$Sequencia.do.PED)]
d=rbind(d,data.frame(de=v[1:(length(v)-1)],para=v[2:length(v)]))
}
d=d[2:nrow(d),]
d=na.omit(d)
g=graph.data.frame(d,directed=TRUE)
g=simplify(g)
elos=get.edgelist(g)
peso=c()
for(rua in elos[,1]){
peso=c(peso,sum(df$Saldo[which(df$Endereco.do.PED==rua)]))
}
E(g)$weight=peso
g3=g
e1=as.data.frame(get.edgelist(g1))
e1=rbind(e1,as.data.frame(get.edgelist(g2)))
e1=rbind(e1,as.data.frame(get.edgelist(g3)))
g=graph.data.frame(e1)
E(g)$weight=c(E(g1)$weight,E(g2)$weight,E(g3)$weight)
g=simplify(g)
G=g
#plot(E(g)$weight)
#Agora vamos fazer a figura da rede
df=DF
v=union(names(V(G)),names(V(G)))
lat=c()
long=c()
for(j in v){
lat=c(lat,dataset$Latitude[which(dataset$Endereco.do.PED==j)[1]])
long=c(long,dataset$Longitude[which(dataset$Endereco.do.PED==j)[1]])
print(length(lat)-length(V(G)))
}
require(leaflet)
require(sp)
meta=data.frame(node=v,lat=lat,lon=long)
#meta=data.frame(node=1:length(D1[,1]),lat=D1[["lat"]],lon=D1[["lon"]])
str(meta)
g=graph.data.frame(as.data.frame(get.edgelist(G)),directed=TRUE,vertices=meta)
gg <- get.data.frame(g, "both")
vert <- gg$vertices
coordinates(vert) <- ~lon+lat
edges <- gg$edges
edges <- lapply(1:nrow(edges), function(i) {
as(rbind(vert[vert$name == edges[i, "from"], ],
vert[vert$name == edges[i, "to"], ]),
"SpatialLines")
})
for (i in seq_along(edges)) {
edges[[i]] <- spChFIDs(edges[[i]], as.character(i))
}
edges <- do.call(rbind, edges)
leaflet(vert) %>% addTiles() %>%
addPolylines(data = edges, weight = 1.5, opacity = 0.15, color = "black", fillOpacity = 0.9) %>%
addCircleMarkers(color = rgb(1,0,0,0.3), alpha = quantile(degree(g)), radius=0.5)
# Informação da rede
c(length(V(G)),length(E(G)))