Skip to content

Instantly share code, notes, and snippets.

View jmcastagnetto's full-sized avatar
🇵🇪
Focusing

Jesus M. Castagnetto jmcastagnetto

🇵🇪
Focusing
View GitHub Profile
@jmcastagnetto
jmcastagnetto / README.md
Last active April 25, 2024 14:22
Example showing the truncation in rio::import() with malformed CSV files
@jmcastagnetto
jmcastagnetto / if_else_ejemplos.R
Created November 20, 2023 15:05
Ejemplo de uso de "if ... else ..." en R
library(tidyverse)
v1 <- LETTERS[1:20]
v1
for (i in 1:length(v1)) {
if (i %% 2 == 0) {
v1[i] <- tolower(v1[i])
} else {
v1[i] <- "-"
}
@jmcastagnetto
jmcastagnetto / barplot-with-labels-on-top.R
Created September 29, 2022 15:45
Simple barplot with labels on top (ggplot2)
library(tidyverse)
library(palmerpenguins)
ggplot(
penguins,
aes(y = species, group = sex, fill = sex)
) +
geom_bar(width = .4) +
geom_text(aes(label = paste("Penguin species:", species), x = 0), nudge_y = .3, hjust = 0, size = 6) +
theme_void() +
@jmcastagnetto
jmcastagnetto / test-pivot-wider.R
Created March 24, 2022 17:53
pivot_wider() on a char column
library(tidyverse)
d <- tibble(
id = c(1, 1, 2, 2),
foo = c("oh", "no", "oh", "no"),
bar = c("hmmm", "ok", "right", "sure?")
)
d %>%
pivot_wider(
@jmcastagnetto
jmcastagnetto / plot-map.R
Created March 11, 2022 21:17
Map showing the asymmetric distribution of the ratio of excess deaths to COVID-19 deaths (2020-2021)
# Data source: http://ghdx.healthdata.org/record/ihme-data/covid_19_excess_mortality
library(tidyverse)
library(sf)
library(rnaturalearth)
library(ggridges)
library(patchwork)
world_map <- ne_countries(type = "countries", returnclass = "sf")
@jmcastagnetto
jmcastagnetto / map-peru-commuting-zones-provinces.R
Last active January 13, 2022 13:33
Plot map of Peru's Commuting Zones and of its provinces as comparison
# Using data from https://data.humdata.org/dataset/commuting-zones
library(tidyverse)
library(sf)
library(geodata)
library(patchwork)
peru_map <- gadm("Peru", level = 2, path = ".") %>%
st_as_sf()
csv <- "~/Downloads/data-for-good-at-meta-commuting-zones-july-2021.csv"
@jmcastagnetto
jmcastagnetto / preproc-data.R
Created January 12, 2022 15:48
Cruzar datos de positivos con vacunados (COVID-19, Peru - basado en datos abiertos del MINSA")
library(tidyverse)
library(arrow)
# RDS de https://github.com/jmcastagnetto/covid-19-peru-limpiar-datos-minsa
positivos <- readRDS("tmp/positivos_covid_aumentado.rds") %>%
select(id_persona, fecha_resultado, metododx) %>%
distinct() %>%
mutate(
id_persona = as.integer(id_persona)
)
@jmcastagnetto
jmcastagnetto / covid19-new-cases-nyt-spiral-graph.R
Created January 10, 2022 20:29
Una versión del gráfico espiral del NYT para los nuevos casos COVID-19 del Perú
# Ref: https://bydata.github.io/nyt-corona-spiral-chart/
# https://github.com/bydata/nyt-corona-spiral-chart/blob/main/spiral-chart.R
library(tidyverse)
library(lubridate)
library(ggtext)
library(patchwork)
owid_url <- "https://github.com/owid/covid-19-data/blob/master/public/data/owid-covid-data.csv?raw=true"
country <- "Peru"
@jmcastagnetto
jmcastagnetto / covid19-peru-positivos-fallecidos.R
Created December 29, 2021 17:41
COVID19 (Perú): Gráficos de positivos y fallecidos por semana epidemiológica
# Inspirado en https://twitter.com/plenque/status/1475934854269259803
library(tidyverse)
library(lubridate)
pos_url <- "https://github.com/jmcastagnetto/covid-19-peru-limpiar-datos-minsa/raw/main/datos/positivos_covid_aumentado.rds"
pos <- rio::import(pos_url)
pos_semana <- pos %>%
mutate(
@jmcastagnetto
jmcastagnetto / 9 - Julia is fast (modified).ipynb
Last active December 6, 2021 13:28
Notebook benchmarking C, Python, Julia and R
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.