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 / who_immunization_tables.R
Created February 6, 2015 15:05
Code to cleanup and read WHO immunization monitoring tables into R data frames.
require(XML)
require(reshape2)
# node content cleanup
cleanup <- function(node) {
val <- xmlValue(node)
if(is.character(val) | is.factor(val)) {
out <- gsub("'","", val, fixed=TRUE)
} else {
out <- val
# Inspired by http://xkcd.com/1609/
set.seed(1609)
food <- c("ice cream", "ham", "relish",
"pancakes", "ketchup", "cheese",
"eggs", "cupcakes", "sour cream",
"hot chocolate", "avocado", "skittles",
"wasabi", "tofu", "steak", "sweet potatoes")
nfood <- length(food)
@jmcastagnetto
jmcastagnetto / brexitchart.R
Last active June 27, 2016 20:02
GBP to USD exchange rate and BREXIT events (2016-06-27)
# BREXIT events and exchange rate of GBP -> USD
library(quantmod)
pd <- getFX("GBP/USD",
from="2016-01-01",
to="2016-06-30")
plot(GBPUSD, ylim=c(1.35,1.5), main="British Pounds per USD")
df <- data.frame(
date = index(GBPUSD),
rate = as.numeric(GBPUSD)

Keybase proof

I hereby claim:

  • I am jmcastagnetto on github.
  • I am jmcastagnetto (https://keybase.io/jmcastagnetto) on keybase.
  • I have a public key whose fingerprint is 73A9 891B 6BAB 0D95 75AF 3759 03B2 0193 CD42 79DF

To claim this, I am signing this object:

@jmcastagnetto
jmcastagnetto / example-conditional-chunk-eval.Rmd
Last active May 30, 2019 14:33
Example of conditional chunk evaluation depending on document output type
---
title: "Example of conditional chunk evaluation"
author: "Jesús M. Castagnetto"
date: "2019-05-30"
output:
pdf_document:
toc: true
number_sections: true
html_document:
df_print: paged
@jmcastagnetto
jmcastagnetto / peru-bandera.R
Created July 11, 2019 14:45
R code to draw the flag of Peru
library(tidyverse)
df <- data.frame(
x1 = 0:2, x2 = 1:3,
y1 = rep(0, 3), y2 = rep(2, 3),
color = c("r", "w", "r"))
ggplot(df) +
geom_rect(aes(xmin = x1, xmax = x2,
ymin = y1, ymax = y2,
fill = color)
@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)
#
# 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 / 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,
@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 %>%