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 / RUT_Calculator.R
Last active December 1, 2023 21:25
Function to calculate the verification digit of a RUT (for Chile)
calculate_dv <- function(rut) {
rut_str <- as.character(rut)
if (!(nchar(rut_str) %in% c(7, 8))) {
stop("The RUT must have 7 or 8 digits.")
}
rut_str <- substr(rut_str, nchar(rut_str) - 7, nchar(rut_str))
rut_rev <- as.numeric(strsplit(rut_str, NULL)[[1]])
@paulovillarroel
paulovillarroel / or_rr.R
Created May 9, 2023 03:16
Brief example for calculating the Odd Ratio (OR) and the Relative Risk (RR).
# Instalamos y cargamos la librería epitools
install.packages("epitools")
library(epitools)
# Casos y controles expuestos
exposed <- c(20, 80)
unexposed <- c(30, 70)
# Creamos una tabla de contingencia
tab <- matrix(c(exposed, unexposed), ncol = 2, byrow = TRUE)
@paulovillarroel
paulovillarroel / chinese_zodiac.py
Last active August 24, 2022 05:20
Function to find the animal and the element of the Chinese zodiac, based on a given year (Python)
## Chinese Zodiac
# More info https://www.travelchinaguide.com/intro/astrology/60year-cycle.htm
def chinese_zodiac(year:int):
elements = ("madera", "fuego", "tierra", "metal", "agua")
animals = ("rata", "buey", "tigre", "conejo", "dragón", "serpiente", "caballo", "oveja", "mono", "gallo", "perro", "cerdo")
if year < 604:
print("El ciclo sexagenario chino comenzó en el año 604. Debes introducir un año adecuado.")
@paulovillarroel
paulovillarroel / chinese_zodiac.R
Last active February 19, 2024 13:10
Function to find the animal and the element of the Chinese zodiac, based on a given year (R)
## Chinese Zodiac
# More info https://www.travelchinaguide.com/intro/astrology/60year-cycle.htm
chinese_zodiac <- function(year) {
elements <- list("madera", "fuego", "tierra", "metal", "agua")
animals <- list("rata", "buey", "tigre", "conejo", "dragón", "serpiente", "caballo", "oveja", "mono", "gallo", "perro", "cerdo")
if (year < 604) {
@paulovillarroel
paulovillarroel / cbind vectors with differents length
Created April 9, 2022 23:44
Small trick for joining vectors of different lengths
library(dplyr)
vec1 <- 1:7
vec2 <- 1:5
cbind(vec1, vec2)
# vec1 vec2
# [1,] 1 1
# [2,] 2 2
# [3,] 3 3
@paulovillarroel
paulovillarroel / webscraping_empleospublicos.R
Last active February 20, 2024 01:52
Webscraping of www.empleospublicos.cl for complete listing of all posted jobs
library(rvest)
library(tidyverse)
library(lubridate)
url <- "https://www.empleospublicos.cl/pub/convocatorias/convocatorias.aspx"
web <- read_html(url)
job <- web |>
html_nodes("#bx_titulos") |>
@paulovillarroel
paulovillarroel / Read multiple worksheets in Excel
Created January 3, 2022 10:26
Read multiple files in Excel with multiple sheets
library(readxl)
library(writexl)
library(tidyverse)
indicators <- read_excel("Talleres/data/Popular_Indicators.xlsx", sheet = "Data")
str(indicators)
colnames(indicators)
unique(indicators$`serie Name`)
tail(indicators)
@paulovillarroel
paulovillarroel / clean_ges_names.R
Last active October 9, 2021 01:39
Example of clearing the name of GES health problems
library(tidyverse)
ges <- ges_df %>%
mutate(problema_de_salud = str_remove(problema_de_salud, pattern = "(\\s?[.]?\\s?[{]\\w+\\s\\w+.\\s?\\d+.?\\d+.)"))
@paulovillarroel
paulovillarroel / compare_scripts
Created September 22, 2021 00:43
Compare two scripts and see the differences between them
#install.packages("diffr")
library(diffr)
diffr("example_script1.R", "example_script_2.R")
@paulovillarroel
paulovillarroel / webscraping_example
Created August 5, 2021 13:51
Automatically obtain the distribution of notebook prices through webscraping
library(rvest)
library(tidyverse)
# Get catalogue
list_products <- data.frame()
for (page_result in seq(from = 1, to = 3, by = 1)) {
url <- paste0("https://www.pcfactory.cl/notebooks?categoria=735&papa=636&pagina=", page_result)