Skip to content

Instantly share code, notes, and snippets.

View paulovillarroel's full-sized avatar
🦄
Coding...

Paulo Villarroel paulovillarroel

🦄
Coding...
View GitHub Profile
@paulovillarroel
paulovillarroel / malignant_neoplasms.R
Created March 24, 2024 03:42
Graph on hospital discharges of malignant neoplasms in Chile (2022)
library(tidyverse)
library(ggdist)
library(glue)
library(ggtext)
#url <- "https://www.fonasa.cl/sites/fonasa/datos-abiertos/bases-grd"
# Unzip file
unzip("raw-data/2022.zip", exdir = "raw-data/")
@paulovillarroel
paulovillarroel / emergency-respiratory-care.R
Created March 5, 2024 00:32
Script to download data of respiratory emergency care in Chile and generate a graph for the care for Influenza disease.
library(tidyverse)
library(arrow)
url <- "https://datos.gob.cl/dataset/606ef5bb-11d1-475b-b69f-b980da5757f4/resource/ae6c9887-106d-4e98-8875-40bf2b836041/download/at_urg_respiratorio_semanal.parquet"
download.file(url, "raw-data/at_urg_respiratorio_semanal.parquet", mode = "wb")
df <- arrow::read_parquet("raw-data/at_urg_respiratorio_semanal.parquet")
influenza_hospitales <- df |>
@paulovillarroel
paulovillarroel / kaplan-meier.R
Created February 24, 2024 22:54
Example of the use of the kaplan-Meier survival analysis model
library(tidyverse)
library(finalfit)
library(survival)
melanoma <- boot::melanoma
melanoma <- melanoma |>
mutate(
status_os = if_else(status == 2, 0,
1
@paulovillarroel
paulovillarroel / births-chile.R
Created February 23, 2024 02:16
Evolution of the number of births in Chile
library(tidyverse)
library(ggdark)
urls <- c(
"https://repositoriodeis.minsal.cl/DatosAbiertos/VITALES/PUBLICADA%20NOV%202022%20MDC/Serie_Nacimientos_1992_2000.zip",
"https://repositoriodeis.minsal.cl/DatosAbiertos/VITALES/PUBLICADA%20NOV%202022%20MDC/Serie_Nacimientos_2001_2019.zip",
"https://repositoriodeis.minsal.cl/DatosAbiertos/VITALES/PUBLICADA%20NOV%202022%20MDC/DA_Nacimientos_2020.zip"
)
file_names <- c("nacimientos1.zip", "nacimientos2.zip", "nacimientos3.zip")
@paulovillarroel
paulovillarroel / survival-analysis-example.R
Created February 22, 2024 02:25
Example of how to perform a survival analysis
# Load necessary libraries
library(tidyverse) # For data manipulation
library(survival) # For survival analysis
library(survminer) # For survival visualization
# Load TidyTuesday data for the 5th week of 2023
data <- tidytuesdayR::tt_load(2023, week = 05)
# Extract relevant datasets
cats_uk <- data$cats_uk
@paulovillarroel
paulovillarroel / life-expectancy-chile.R
Created February 17, 2024 22:26
Code showing the evolution of Chile's life expectancy (source OECD)
library(tidyverse)
url <- "https://sdmx.oecd.org/public/rest/data/OECD.ELS.HD,DSD_HEALTH_STAT@DF_LE,1.0/all?dimensionAtObservation=AllDimensions&format=csvfilewithlabels"
data <- read_csv(url)
first_last_values <- data |>
filter(
`Reference area` == "Chile",
Measure == "Life expectancy",
@paulovillarroel
paulovillarroel / programming-languages-github.R
Created February 11, 2024 02:17
The script visualizes the popularity trends of "R" and "Python" programming languages in Chile.
library(tidyverse)
# URL for the dataset
url <- "https://raw.githubusercontent.com/github/innovationgraph/main/data/languages.csv"
# Read the dataset
data <- read_csv(url)
# Perform data manipulation and aggregation
p <- data |>
@paulovillarroel
paulovillarroel / data_gob_cl_API
Created January 16, 2024 04:44
Function to use the data.gob.cl API
library(httr)
library(jsonlite)
library(dplyr)
url <- "https://datos.gob.cl/api/3/action/datastore_search?resource_id=2c44d782-3365-44e3-aefb-2c8b8363a1bc"
# Option A: paginated download
download_paginated_data <- function(url) {
all_data <- list()
offset <- 0
@paulovillarroel
paulovillarroel / central_limit_theorem.R
Created January 7, 2024 22:17
Small script to explain the central limit theorem
set.seed(2006)
sample_sizes <- c(1, 10, 30, 1000)
nsims <- 10000
# Helper function: Mean of one sample of X
one_mean <- function(n, p = c(0.8, 0.2)) {
mean(sample(0:1, n, replace = TRUE, prob = p))
}
@paulovillarroel
paulovillarroel / RUT_Calculator.py
Created December 1, 2023 21:25
Function to calculate the verification digit of a RUT (for Chile)
def calculate_dv(rut):
rut_str = str(rut)
if len(rut_str) not in [7, 8]:
raise ValueError("The RUT must have 7 or 8 digits.")
rut_str = rut_str.zfill(8)[-8:]
rut_rev = list(map(int, list(rut_str[::-1])))