Skip to content

Instantly share code, notes, and snippets.

View damianooldoni's full-sized avatar
🚴‍♂️

Damiano Oldoni damianooldoni

🚴‍♂️
View GitHub Profile
@damianooldoni
damianooldoni / apply_str_squish_to_all_df.R
Last active August 7, 2018 13:38
Removes whitespace from start, end and repeated whitespaces inside all dataframe.
library(dplyr)
library(stringr)
df <- data.frame(a = c(" aZe aze s", "wxc s aze "),
b = c(" 12 12 ", "34e e4 "),
stringsAsFactors = FALSE)
df <- df %>%
rowwise() %>%
mutate_all(funs(str_squish(.))) %>%
ungroup()
@damianooldoni
damianooldoni / equals_sign_cheat_sheet.py
Created August 17, 2018 21:35
The Equals sign cheat sheet showing the behavior of "=" with basic classes (integers, tuples, lists, sets, dictionaries)
# integers, tuples: "=" makes a copy of the object. Changes of the original object after "=" instruction doesn't affect the copy.
a = 2
b = a
a = 3
print(a) # 3
print(b) # 2
# tuples
a = (2, 3)
b = a
@damianooldoni
damianooldoni / Maximum Heart Rate Calculation
Last active October 7, 2018 14:13
Function to calculate the theoretical maximum Heart Rate (HRmax)
#' Function to caclulate the theoretical maximum Heart Rate (HRmax)
#'
#' @param sex (character) "M" or "F"
#' @param age (numeric) Age in years
#' @param weight (numeric) Weight in kilograms
#'
#' @return theoretical maximum Heart Rate
#' @examples
#' get_HRmax(sex = "M", age = 35, weight = 61)
#'
@damianooldoni
damianooldoni / get_df_with_issues
Last active October 8, 2018 09:41
name_usage() returns issues as codes. This code shows how replace the codes with the corresponding issues
devtools::install_github("ropensci/rgbif")
library(rgbif)
library(tidyverse)
library(rgbif)
# GBIF taxon keys (from Manual of Alien Plants of Belgium)
keys <- c(141264614, 8421432)
# The issues are returned as codes
example_duplicates <- map_df(keys,
@damianooldoni
damianooldoni / check_correctness_GBIF_rgbif_downloads.Rmd
Last active October 18, 2018 11:56
Check whether number of occurrences for some species is the same for 6 different retrieval channels: GBIF website interface, GBIF csv download, GBIF DwCA download, csv as retrieved by `rgbif`, DwcA as retrieved by `rgbif`. There is also a final check taking into account a R-based way to trigger a query (`rgbif::occ_download()`).
---
title: "Compare CSV and DwC files from GBIF internetsite, GBIF manual downloads and `rgbif` authomatic downloads"
author:
- Damiano Oldoni
date: "October 16, 2018"
output:
html_document:
toc: true
toc_depth: 3
toc_float: true
@damianooldoni
damianooldoni / check_correctness_occurrences_with_hasCooridnate.Rmd
Last active October 18, 2018 12:26
If we add the parameter `hasCoordinate=TRUE` in the rgbif query to GBIF, we should get less or equal number of occurrences. This gist will check it.
---
title: "Check correctness downloads with query parameter `hasCoordinate`"
author:
- Damiano Oldoni
date: "October 18, 2018"
output:
html_document:
toc: true
toc_depth: 3
toc_float: true
@damianooldoni
damianooldoni / mutate_via_pmap_purrr.R
Last active December 6, 2018 13:18
New way of working row-wise in R via purrr package
#' Change value of a column (\code{remarks}) for rows with some fields equal to some rows in a second df (called here \code{df_ref}).
#' Notice that all \code{pmap()} family of functions demands same number of columns as the input df.
#' It is strongly recommended to use the column names as inputs of the function of \code{pmap()}.
#' In this specific example we require that \code{col3} is of type \code{character} and we use \code{pmap_chr()} to return a character.
library(purrr)
library(dplyr)
df <-
df %>%
@damianooldoni
damianooldoni / type_GBIF_occurrence_fields.R
Last active December 7, 2018 11:47
Type specification of fields of GBIF occurrence downloads
library(readr)
col_types <- cols(
acceptedNameUsage = col_character(),
acceptedNameUsageID = col_character(),
accessRights = col_character(),
associatedReferences = col_character(),
associatedSequences = col_character(),
associatedTaxa = col_character(),
bed = col_character(),
behavior = col_character(),
@damianooldoni
damianooldoni / use_of_filter_at.R
Created December 12, 2018 14:48
Scoped functions: use of filter_at(). Check https://dplyr.tidyverse.org/reference/filter_all.html for more.
# Example 1: select rows of data with NA in all columns starting with substring bb_:
filtered_data <-
data %>%
filter_at(vars(starts_with("bb_")), all_vars(is.na(.)))
# Example 2: filter rows of data with NA in one of the columns starting with substring bb_:
filtered_data <-
data %>%
filter_at(vars(starts_with("bb_")), any_vars(is.na(.)))
@damianooldoni
damianooldoni / mutate_with_char_var_as_col_name.R
Last active January 29, 2019 12:30
Use of `dplyr::mutate` to add a column by character variable as column name
# Dummy df
df_1 <- tibble::tibble(col1 = c("a","b"),
col2 = c(1,2))
# Name new column
new_col <- "col3"
# Add new column
dplyr::mutate(df_1, !!new_col := paste0(col1, ":", col2, "."))