Skip to content

Instantly share code, notes, and snippets.

@mpaulacaldas
mpaulacaldas / year-quarter-format.R
Created September 21, 2023 17:10
year-quarter format with scale_x_date()
# scales::date_format() only relies on strptime(), which does not support
# quarter formatting. stringi does, but the format strings have a different
# syntax
quarter_format <- function(x, format = "yyyyqqq", tz = "UTC", locale = "en_EN") {
function(x) stringi::stri_datetime_format(time = x, format, tz, locale)
}
# Example, inspired by the ggplot2 docs
library(ggplot2)
@mpaulacaldas
mpaulacaldas / convert-heic-to-jpeg.R
Created February 14, 2022 22:22
Convert HEIC to JPEG using magick
library(magick)
library(tidyverse)
path_heics <- dir("~/Downloads/", pattern = "HEIC$", full.names = TRUE)
path_jpeg <- str_replace(path_heics, "HEIC", "jpeg")
# convert before writing
heics <- map(path_heics, image_read)
jpeg <- map(heics, image_convert, format = "jpeg")
walk2(jpeg, path_jpeg, image_write, quality = 50)
@mpaulacaldas
mpaulacaldas / dplyr-to-datatable_summarise-across.md
Last active March 18, 2021 13:42
How to group_by() + summarise() + across() in data.table
library(dplyr)
library(data.table)

# dplyr across ------------------------------------------------------------

iris %>% 
  group_by(Species) %>% 
  summarise(across(c(Sepal.Length, Sepal.Width), tibble::lst(mean, median)))
#> `summarise()` ungrouping output (override with `.groups` argument)
@mpaulacaldas
mpaulacaldas / info-pkgs.R
Created April 26, 2020 21:43
Get info on installed packages
library(dplyr)
library(stringr)
library(sessioninfo)
libpath <- .libPaths()[[1]]
pkgs <- basename(dir(libpath))
info <- package_info(pkgs)
github <- info %>%
@mpaulacaldas
mpaulacaldas / rowwise-pmap.md
Last active March 31, 2020 17:13
Calculating the number of missing values per row can be very tricky with pmap. It's better to use rowwise().
# Question from the R-Ladies Slack Channel

library(tidyverse)

test_data <- tibble::tribble(
  ~c1, ~c2, ~c3, ~c4,
  1, 2, 2, 4,
  3, 4, 3, 4,
  NA_integer_, 4, 4, 3,
@mpaulacaldas
mpaulacaldas / rename-bangbangbang.md
Created May 13, 2019 15:31
Reminder rename + !!!
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#&gt; intersect, setdiff, setequal, union

Keybase proof

I hereby claim:

  • I am mpaulacaldas on github.
  • I am mpaulacaldas (https://keybase.io/mpaulacaldas) on keybase.
  • I have a public key ASCvCYR-gM2TLCBMqFc-BLvw-yJxcE7jwOmNy6NT6Nii1Ao

To claim this, I am signing this object: