Skip to content

Instantly share code, notes, and snippets.

View rafapereirabr's full-sized avatar

Rafael H M Pereira rafapereirabr

View GitHub Profile
@rafapereirabr
rafapereirabr / radiation_model.R
Last active November 1, 2021 20:11
Calculate the ratiation model in R
#' Code by Rafael H. M. Pereira and Marcus Saraiva
#'
#' Radiation model (Simini et al 2012)
#' Tij = Ti * ((Pi * Pj) / ((Pi + Pij) * (Pi + Pj + Pij)))
#'
#' Where:
#'
#' Tij: Avarage flux between origin i and destination j predicted by the radiation model
#' Ti: Proportion of population in i relative to total population
#' Pi: Absolute population in i
@rafapereirabr
rafapereirabr / test gtfs2gps.R
Created October 27, 2021 18:01
test gtfs2gps
library(gtfs2gps)
library(gtfsrouter)
# original process
sp <- system.file("extdata/saopaulo.zip", package ="gtfs2gps")
time_gps <- system.time(gps_gps <- gtfs2gps(sp, parallel = T, spatial_resolution = 200))
@rafapereirabr
rafapereirabr / pride_brazil_map_geobr.R
Last active June 29, 2021 22:54
Create rainbow color map of Brazil in R
library(geobr)
library(ggplot2)
library(sf)
library(sfheaders)
df <- read_municipality(year=2018)
centrd <- st_centroid(df)
centrd <- sfheaders::sf_to_df(centrd, fill=T)
df$position <- centrd$y + centrd$x
@rafapereirabr
rafapereirabr / map_blog_visitors2.R
Created June 28, 2021 17:36
Density map of website visitors
library(googleAnalyticsR)
library(data.table)
library(ggplot2)
library(dplyr)
library(rworldmap)
library(sf)
options(scipen=999)
## setup
ga_auth()
@rafapereirabr
rafapereirabr / geocode_google_api.md
Created September 18, 2020 18:51
geocode_google_api

library(ggmap)
library(data.table)

# registrar Google API Key
register_google(key = 'xxxxx-xxxxxx-xxxxx-xxxxx')
library(geobr)
library(sf)
library(mapview)
library(stringr)
mapviewOptions(platform = 'leafgl')
# read original census tracts
urban_cts <- read_census_tract(year = 2000, code_tract = 'RJ', zone ='urban', simplified = F)
rural_cts <- read_census_tract(year = 2000, code_tract = 'RJ', zone ='rural', simplified = F)

A simple reproducible Hexsticker for r5r, a package for Rapid Realistic Routing with R5 in R.

### Libraries ------------------------
remotes::install_github("GuangchuangYu/hexSticker")

library(hexSticker) # https://github.com/GuangchuangYu/hexSticker
library(ggplot2)
library(sf)
@rafapereirabr
rafapereirabr / dist_to_border.md
Last active August 30, 2020 10:41
Calculating distance between spatial object and the nearest border

Calculating distance between spatial object and the nearest border

Quick example showing how to calculate the distance from each census tract to the nearest muncipality border

library(sf)
library(furrr)
library(geobr)
library(mapview)
library(magrittr)
library(data.table)
@rafapereirabr
rafapereirabr / quantile_by_groups.md
Last active July 23, 2020 15:31
Create quantile by groups using data.table

quick code snippet to create (weighted) quantiles in R using data.table.

Create reproducible example

library(data.table)
library(Hmisc)

# create data.table
n &lt;- 100000
@rafapereirabr
rafapereirabr / assign_NA_to_neighbor.R
Last active January 29, 2021 19:13
Assign NA value to neighbor polygon with largest pop
```
library(sf)
library(geobr)
# sample spatial data
df <- read_state()
# create random pop and cases
df$pop <- sample(1:27, 27, replace = T)