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
Created January 19, 2021 03:24
RStudio Global 2021 events
# based on https://gist.github.com/andrewheiss/5cb3ec07be2b1bf5dea8806dfaa755e4
# With minor tweaks and translation to Spanish
library(tidyverse)
library(showtext)
font_add_google("Fira Sans Condensed", "firasanscond")
font_add_google("Fira Sans Extra Condensed", "firasansextracond")
showtext_auto()
@jmcastagnetto
jmcastagnetto / test_new_km_survminer.R
Last active March 3, 2020 23:35
Trying to make something like Fig 5 from http://dx.doi.org/10.1136/bmjopen-2019-030215 using gridExtra, cowplot and survminer (ref: https://twitter.com/tmorris_mrc/status/1234946869362601984)
library(tidyverse)
library(survminer)
library(survival)
library(cowplot)
library(gridExtra)
fit <- survfit(Surv(futime, fustat) ~ rx, data = ovarian)
p1 <- ggsurvplot(
fit,
@jmcastagnetto
jmcastagnetto / stemming-snowballc-hunspell.R
Created February 12, 2020 21:54
Stemming with SnowballC vs hunspell
#ref: https://github.com/juliasilge/tidytext/issues/17
library(dplyr)
library(hunspell)
library(SnowballC)
w <- tibble(
palabras = c(
"celebra",
"celebré",
@jmcastagnetto
jmcastagnetto / 20200127-Parti_ciu_TODOS_DETALLE.csv
Last active January 28, 2020 21:30
Gráfico simple de las tasas de ausentismo/abstencionismo electoral -- Elecciones Congreso 2020 (Perú)
We can make this file beautiful and searchable if this error is corrected: It looks like row 4 should actually have 1 column, instead of 5. in line 3.
#Participación Ciudadana Elecciones Congresales Extraordinarias 2020
#Ubigeo: TODOS
#ACTUALIZADO EL 27/01/2020 A LAS 12:08 h
#,PARTICIPACIÓN,,AUSENTISMO,
"DEPARTAMENTO/CONTINENTE","TOTAL ASISTENTES","% TOTAL ASISTENTES","TOTAL AUSENTES","% TOTAL AUSENTES","ELECTORES HÁBILES"
"AFRICA","0","0.00","0","0.00","0"
"AMAZONAS","157,470","63.68","89,827","36.32","247,297"
"AMERICA","0","0.00","0","0.00","0"
"ANCASH","570,090","73.25","208,165","26.75","778,255"
"APURIMAC","200,529","69.24","89,105","30.76","289,634"
@jmcastagnetto
jmcastagnetto / peru-uit-cambio.R
Created January 15, 2020 16:34
Cambio del valor de una UIT en el Perú a lo largo del tiempo
library(tidyverse)
library(rvest)
url <- "http://www.sunat.gob.pe/indicestasas/uit.html"
uit_table <- html(url) %>%
html_node(xpath = "/html/body/div[2]/div/div[1]/div[4]/div[1]/div[1]/center/table") %>%
html_table(header = TRUE) %>%
janitor::clean_names()
uit_df <- uit_table %>%
@jmcastagnetto
jmcastagnetto / sfs-index-south-america.R
Last active November 25, 2019 19:53
Plot of the SFS index for South America
library(countrycode)
library(tidyverse)
library(ggrepel)
df <- readxl::read_excel("Sustainable_food_systems_global_index-1.xlsx", sheet = 1, skip = 5)
df <- df %>%
mutate(
region = countrycode(origin = "iso3c",
sourcevar = ISO3,
# Ref: https://twitter.com/Amit_Levinson/status/1187754568014336002
set.seed(2019)
gen_grades <- function(n, avg, sd, min, max, nsmpl) {
smpl <- rnorm(nsmpl, avg, sd)
smpl <- smpl[smpl >= min & smpl <= max]
if (length(smpl) >= n) {
return(sample(smpl, n))
} else {
stop(simpleError("Not enough sample data. Increase 'nsmpl'"))
@jmcastagnetto
jmcastagnetto / echarts4r-treemap-example.R
Last active November 18, 2021 15:22
Example of use of echarts4r to make a treemap
# based on the example at: https://echarts4r.john-coene.com/articles/chart_types.html#treemap
# the JS example at: https://echarts.apache.org/examples/en/editor.html?c=treemap-show-parent
# and the documentation at: https://echarts.apache.org/en/option.html#series-treemap.type
library(tidyverse)
library(echarts4r)
# this data structure does not work with the changed function
# set.seed(13579)
#