Skip to content

Instantly share code, notes, and snippets.

library(tidyverse)
n <- 1000
v <- 1:n
vector_c <- function(n) {
out <- c()
for (i in 1:n) {
out <- c(out, i)
}
@jrosell
jrosell / anomalies.qmd
Last active August 23, 2023 15:57
Detect anomalies over time using percentiles and using a Isolation forest model.
---
title: "anomalies"
format: html
jupyter: python3
editor_options:
chunk_output_type: console
---
Install requisites:
@jrosell
jrosell / anomalies.R
Last active August 23, 2023 10:53
Detect anomalies over time using percentiles and using a GAM model with a local smoother or Isolation Forest model
# GAM model with a local smoother
library(tidyverse)
set.seed(2)
elapsed <- arima.sim(model = list(order = c(0, 1, 0)), n=200) + 20
elapsed <- pmax(elapsed, 1)
data <- tibble(
x = 1:201,
elapsed = elapsed
)
plot(data)
@jrosell
jrosell / base-r-summarise.R
Last active March 20, 2024 15:44
How would you write this in base R 4.1+? Is there a better way? More approaches here: https://gist.github.com/hadley/c430501804349d382ce90754936ab8ec
# Single grouping in base R: Multiple functions
library(dplyr, warn.conflicts = FALSE)
expected_output <- mtcars %>%
group_by(cyl) %>%
summarise(mean = mean(disp), n = n()) %>%
as.data.frame()
expected_output
by <- c("cyl")
<VRTDataset rasterXSize="10" rasterYSize="10">
<SRS dataAxisToSRSAxisMapping="2,1">GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]</SRS>
<GeoTransform> 0.0000000000000000e+00, 1.0000000000000000e+00, 0.0000000000000000e+00, 9.0000000000000000e+01, 0.0000000000000000e+00, -1.8000000000000000e+01</GeoTransform>
<VRTRasterBand dataType="Float32" band="1">
<NoDataValue>nan</NoDataValue>
<ColorInterp>Gray</ColorInterp>
<ComplexSource>
<SourceFilename relativeToVRT="0">rasters/raster.tif</SourceFilename>
<SourceBand>1</SourceBand>
<SourceProperties RasterXSize="10" RasterYSize="10" DataType="Float32" BlockXSize="10" BlockYSize="10" />
@jrosell
jrosell / combine_multylayered_raster_vtr.R
Created July 30, 2023 08:49
Combine multilayered_raster vtr using terra package
# combine_multylayered_raster_vtr.R: Combine multilayered_raster vtr using terra package
library(terra)
test <- TRUE
test_create_vrt_from_multilayers <- function() {
raster_objects <- create_multiplayered_rasters()
print(class(raster_objects[[1]]) == "SpatRaster")
print(class(raster_objects[[2]]) == "SpatRaster")
out <- create_vrt_from_multilayers(raster_objects)
print(out)
@jrosell
jrosell / tasa_paro.R
Last active July 18, 2023 14:45
Visualizando la tasa de paro diaria y anual de USA desde 1980 en R. Un ejemplo de uso de los paquetes readr, tibble, magrittr, dplyr, ggplot2, scales y lubridate de tidyverse.
library(tidyverse)
economics_raw <- read_csv("https://gist.githubusercontent.com/jrosell/cf35e54a70914a3b23946c0a0d3a2cee/raw/47e52b910ecb23c3c7a8c549c9fd277797bd9970/economics_long.csv")
print(economics_raw, n = 5)
glimpse(economics_raw)
economics_raw %>%
glimpse(x = .)
date variable value value01
1967-07-01 pce 506.7 0
1967-08-01 pce 509.8 2.652497197765077e-4
1967-09-01 pce 515.6 7.615233890357775e-4
1967-10-01 pce 512.2 4.7060434153896666e-4
1967-11-01 pce 517.4 9.155393553576157e-4
1967-12-01 pce 525.1 0.001574385433512166
1968-01-01 pce 530.9 0.002070659102771431
1968-02-01 pce 533.6 0.002301683052254198
1968-03-01 pce 544.3 0.003217222407611809
@jrosell
jrosell / pak.R
Last active July 18, 2023 13:56
Instalación del paquete {pak} y de paquetes de tidyverse en R.
# install.packages("pak")
stopifnot(
'{pak} no está instalado. Por favor, ejecuta install.packages("pak") en la consola y vuelve a intentarlo.' =
sum("pak" == row.names(installed.packages())) == 1
)
instalar <- setdiff(
c("ggplot2", "dplyr", "tidyr", "readr", "purrr", "tibble", "stringr", "forcats", "lubridate", "magrittr", "tidyverse"),
rownames(installed.packages())
)
@jrosell
jrosell / irpf-espana-2023.R
Last active August 10, 2023 13:39
Calculando el rendimiento después de IRPF en España 2023 según detallado aquí https://www.bankinter.com/blog/finanzas-personales/como-calcular-irpf-caso-practico
# Hasta 12450 euros: 19 %
# De 12.450 euros hasta 20.200 euros: 24 %
# De 20.200 euros hasta 35.200 euros: 30 %
# De 35.200 euros hasta 60.000 euros: 37 %
# De 60.000 euros hasta300.000 euros: 45 %
# Más de 300.000 euros: 47 %
library(tidyverse)
library(patchwork)