Skip to content

Instantly share code, notes, and snippets.

@idshklein
Created March 14, 2022 20:11
Show Gist options
  • Save idshklein/f7ce6a228c49be2acaf97a5b15ede9ce to your computer and use it in GitHub Desktop.
Save idshklein/f7ce6a228c49be2acaf97a5b15ede9ce to your computer and use it in GitHub Desktop.
library(tidyverse)
library(readxl)
library(sf)
library(igraph)
library(tidygraph)
library(mapview)
nat <- read_csv("C:/idos_shit/celular1819_v1/v_1/AvgDayHourlyTrips201819_1270_weekday_v1.csv")
# nat <- read_csv("C:/idos_shit/celular1819_v1/v_1/AvgDayHourlyTrips201819_1270_friday_v1.csv")
# nat <- read_csv("C:/idos_shit/celular1819_v1/v_1/AvgDayHourlyTrips201819_1270_saturday_v1.csv")
shp1 <- st_read("C:/idos_shit/celular1819_v1/v_1/Shape files/1270_02.09.2021.shp",options = "ENCODING=WINDOWS-1255")
clusv21 <- nat %>%
gather(hour,count,-fromZone,-ToZone) %>%
rename(from = fromZone,to = ToZone) %>%
# filter(hour == "h2") %>%
group_by(from,to) %>%
summarise(count = sum(count)) %>%
ungroup() %>%
as_tbl_graph(directed = F)
with_grps <- clusv21 %E>%
filter(count >= 0) %N>%
mutate(grp = group_louvain(weights = count))
anal <- with_grps %E>%
as_tibble() %>%
left_join(with_grps %>% as_tibble() %>% mutate(rn = row_number()),by = c("from"="rn")) %>%
left_join(with_grps %>% as_tibble() %>% mutate(rn = row_number()),by = c("to"="rn")) %>%
group_by(grp.x,grp.y) %>%
summarise(count = sum(count)) %>%
mutate(perc = count/sum(count),
same = grp.x==grp.y) %>%
filter(same)
# ungroup() %>%
# group_by(same) %>%
# summarise(count = sum(count)) %>%
with_grps %>%
as_tibble() %>%
group_by(grp) %>%
add_count() %>%
ungroup() %>%
filter(n>1) %>%
left_join(shp1 %>%mutate(TAZ_1270 = as.character(TAZ_1270)) ,by = c("name" = "TAZ_1270")) %>%
left_join(anal,by = c("grp"="grp.x")) %>%
# filter(grp == 48) %>%
st_sf() %>%
group_by(grp) %>%
# summarise(perc = mean(perc)) %>%
mapview(zcol = "grp",col.regions = randomcoloR::randomColor(1000))
@baobabprince
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment