Skip to content

Instantly share code, notes, and snippets.

View BlasBenito's full-sized avatar

Blas Benito BlasBenito

View GitHub Profile
#impute NA with group mean
df <- df %>%
group_by(
grouping_column_1,
grouping_column_2
) %>%
mutate_at(
all_of(target_columns),
~ ifelse(is.na(.), mean(., na.rm = TRUE), .)
)
#!/bin/bash
#reprojects in parallel geotif files in MODIS sinusoidal
#in the "raw" folder
#to geotif files in EPSG 4326 in the current folder
#with the suffix "_wgs_84"
#listing files in raw
for FILEPATH in raw/*; do
#generates output file name
#!/bin/bash
#reprojects a geotif from MODIS sinusoidal to EPSG 4326
#check https://gdal.org/programs/gdalwarp.html for further details
gdalwarp \
-of GTIFF \
-multi \
--config GDAL_CACHEMAX 1024 \
-s_srs '+proj=sinu +R=6371007.181 +nadgrids=@null +wktext' \
-t_srs '+proj=longlat +datum=WGS84 +no_defs' \
-r near \
@BlasBenito
BlasBenito / Makefile
Created August 10, 2022 16:54 — forked from mpneuried/Makefile
Simple Makefile to build, run, tag and publish a docker containier to AWS-ECR
# import config.
# You can change the default config with `make cnf="config_special.env" build`
cnf ?= config.env
include $(cnf)
export $(shell sed 's/=.*//' $(cnf))
# import deploy config
# You can change the default deploy config with `make cnf="deploy_special.env" release`
dpl ?= deploy.env
include $(dpl)
#R script to plot the CRAN download history of a set of packages
library(dlstats)
library(ggplot2)
target.packages <- c("ranger", "randomForest", "xgboost")
x <- dlstats::cran_stats(packages = target.packages)
ggplot2::ggplot(data = x) +
#This script downloads soilgrids (https://www.isric.org/explore/soilgrids) data
#at a given resolution (variable "res", in degrees) and the depth intervals 0-5 and 5-15
#to compute the mean for the depth interval 0-15.
#It also applies the conversion factor shown in the FAQ (https://www.isric.org/explore/soilgrids/faq-soilgrids#How_can_I_access_SoilGrids)
#to reconstitute the original data units.
library(gdalUtils)
library(raster)
#variables to download
@BlasBenito
BlasBenito / engineering_features.R
Last active May 19, 2021 14:01
Shows how spatialRF::rf_interactions() works to find variable combinations (via multiplication and PCA) that improve model transferability.
#installing the development version of the package (v.1.1.1)
remotes::install_github(
repo = "blasbenito/spatialRF",
ref = "development",
force = TRUE,
quiet = TRUE
)
library(spatialRF)
#loading the example data
@BlasBenito
BlasBenito / multicollinearity.R
Last active May 19, 2021 08:40
Use of spatialRF::auto_cor() and spatialRF::auto_vif() to reduce multicollinearity in a set of predictors while taking into account a preference order defined by the user..
#installing and loading package
remotes::install_github(
repo = "blasbenito/spatialRF",
ref = "main",
force = TRUE,
quiet = TRUE
)
library(spatialRF)
library(magrittr)
@BlasBenito
BlasBenito / interactions.R
Created March 25, 2021 20:56
Shows how variable interaction shapes change with scaling or a variable having 0 in its range
#preparing data
library(car)
data("Prestige")
df <- Prestige[, c(
"income",
"education",
"prestige"
)]
#making a version of education with its range crossing 0
@BlasBenito
BlasBenito / functions_betadiversity.R
Last active January 7, 2021 08:30
Functions to compute Simpson's and Sorensen's betas from taxa lists from different sites
#returns biodiversity components a, b, and c from two vectors with taxa names.
#x: character vector, taxa list of one site
#y: character vector, taxa list of another site
abc <- function(x, y){
#list to store output
out <- list()
#filling the list
out$a <- length(intersect(x, y))