Skip to content

Instantly share code, notes, and snippets.

@ursulams
ursulams / day_1.R
Last active December 3, 2023 01:25
2023 advent of code day 1
# first star
df <- read.table(text = puzzle_input, header = FALSE, sep = "\n")
df$digits <- as.numeric(paste0(sub(".*?(\\d).*", "\\1", df$V1), sub(".*(\\d+).*$", "\\1", df$V1)))
sum_cal_values <- sum(df$digits)
# second star
df$V2 <- sapply(df$V1, function(x){
gsub("one", "o1e",
gsub("two", "t2o",
gsub("three", "thr3e",
@KaiAragaki
KaiAragaki / my-workflow.txt
Last active June 8, 2023 13:05
My workflow
Almost all - if not all - of my new R projects are targets projects.
I'm usually bouncing between computers and occasionally have a collaborator, so targets makes it super super simple to spin up
my analyses at another computer.
Nowadays, I don't just use a targets project, but I like having multiple targets projects within one targets project. This
keeps the pipelines from getting to unweildly, at which point I usually get pretty overwhelmed.
(From here on, assume that when I say 'project' I mean a targets project, not an RStudio project - all of this happens in a
single RStudio project)
@AlbertRapp
AlbertRapp / label_cleaning.R
Created February 18, 2023 14:18
3 ways to relabel data
library(tidyverse)
big_tech_companies <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-02-07/big_tech_companies.csv')
big_tech_companies
# # A tibble: 14 × 2
# stock_symbol company
# <chr> <chr>
# 1 AAPL Apple Inc.
# 2 ADBE Adobe Inc.
# 3 AMZN Amazon.com, Inc.
# 4 CRM Salesforce, Inc.
@strengejacke
strengejacke / .lintr
Last active May 5, 2024 17:20
VS Code setup for R
// save to windows-user directory
linters: with_defaults(object_name_linter = NULL,
object_length_linter(50),
commented_code_linter = NULL,
object_usage_linter = NULL,
line_length_linter(120),
cyclocomp_linter = cyclocomp_linter(50))
library(dplyr)
library(arrow)
library(duckdb)
# install.packages(c("dplyr", "arrpw", "duckdb", "nycflights13"))
## Export the nycflights13 dataset to arrow format ----
nycflights13::flights |>
@AlbertRapp
AlbertRapp / stacked_bar_plot_alternative.qmd
Last active June 27, 2023 13:01
How to split bars instead of stacking them
---
output: html_document
editor_options:
chunk_output_type: console
---
```{r}
setwd(here::here('stacked_bar_alternative/'))
camcorder::gg_record(
dir = 'img',
@z3tt
z3tt / penguins_rainclouds.R
Created July 3, 2022 18:30
Polished raincloud plot using the Palmer penguins data
library(palmerpenguins)
library(ggtext)
library(colorspace)
library(ragg)
url <- "https://raw.githubusercontent.com/allisonhorst/palmerpenguins/master/man/figures/lter_penguins.png"
img <- magick::image_read((url))
pic <- grid::rasterGrob(img, interpolate = TRUE)
pal <- c("#FF8C00", "#A034F0", "#159090")
options(reactable.theme = reactable::reactableTheme(
color = "hsl(233, 9%, 87%)",
backgroundColor = "#002b36",
borderColor = "#eee8d5",
stripedColor = "#586e75",
highlightColor = "#6c71c4",
inputStyle = list(backgroundColor = "hsl(233, 9%, 25%)"),
selectStyle = list(backgroundColor = "hsl(233, 9%, 25%)"),
pageButtonHoverStyle = list(backgroundColor = "hsl(233, 9%, 25%)"),
pageButtonActiveStyle = list(backgroundColor = "hsl(233, 9%, 28%)")
@edonnachie
edonnachie / sf_map_overlay.R
Last active May 9, 2022 21:57
Demonstration of how to overlay one region onto another using R and sf
library(sf)
library(ggplot2)
# Data from https://www.geoboundaries.org/index.html#getdata
de2 <- read_sf("data/geoBoundaries-DEU-ADM2-all/geoBoundaries-DEU-ADM2_simplified.shp")
# Extract Munich and Berlin to separate objects
munich <- dplyr::filter(de2, DistrictCo == "09162")
berlin <- dplyr::filter(de2, DistrictCo == "11000")
CreateProjectFiles <- function(PROJECT_PATH = rstudioapi::getActiveProject(),
FOLDERS_TO_CREATE = c("data", "docs", "figs", "logs",
"output", "queries", "R", "tests"),
OPEN_NEW_SESSION = TRUE,
DEBUG = TRUE){
if(DEBUG) message("CreateProjectFiles: Function initialized \n")
if(!dir.exists(PROJECT_PATH)){