Skip to content

Instantly share code, notes, and snippets.

View MCMaurer's full-sized avatar

Michael Culshaw-Maurer MCMaurer

View GitHub Profile
@MCMaurer
MCMaurer / combine_dups.R
Created October 18, 2022 17:59
combining distinct values from a column where rows are otherwise duplicates
library(tidyverse)
tibble(group = c("A", "A", "B", "C"),
major = c("bio", "chem", "bio", "art")) %>%
group_by(group) %>%
summarise(major = str_c(major, collapse = "/")) %>%
separate(major, into = c("major1", "major2"), sep = "/")
@MCMaurer
MCMaurer / manual_colors_in_ggplot.R
Created October 18, 2022 16:41
How to grab colors to use in ggplot
library(tidyverse)
mtcars %>%
ggplot(aes(x = wt, y = mpg, color = factor(cyl))) +
geom_point() +
scale_color_manual(values = c("red", "blue", "#80DBCA"))
# open Digital Color Meter on Mac, find the color you want, and get the RGB values from that application
# then put those colors in here, and use the resulting hex code in scale_color_manual
@MCMaurer
MCMaurer / range_change_plots.R
Last active January 24, 2022 22:48
Generating range change plots in ggplot
library(tidyverse)
# much better way to do this ---------------
d <- tribble(
~species, ~low_1, ~high_1, ~low_2, ~high_2, ~shift,
"PRLC", 180, 740, 180, 990, "expansion",
"AMRE", 640, 690, 250, 780, "expansion",
"PETH", 180, 990, 180, 840, "contraction",
"PRBU", 180, 1000, 180, 1000, "no_change",
@MCMaurer
MCMaurer / pairwise_col_diffs.R
Created July 6, 2021 17:38
pairwise column differences
library(tidyverse)
library(tidybayes)
d <- tibble(sensor_a = rnorm(100, 5, 2),
sensor_b = rnorm(100, 1, 1),
sensor_c = rnorm(100, 0, 3),
time = 1:100)
# pivot longer so all our sensor names are in one column, and readings in another
d <- d %>%
library(tidyverse)
library(brms)
library(tidybayes)
d <- tibble(a = rnorm(1000, sd = 1),
b = rnorm(1000, sd = 5),
c = rnorm(1000, sd = 2),
d = rnorm(1000, sd = 1),
e = rnorm(1000, sd = 3))
@MCMaurer
MCMaurer / evil_functions
Last active May 25, 2021 20:29
a demonstration of some of the crazy stuff you can do renaming functions in R
attach(list(
`+` = function(a, b){
noquote(paste0(sum(a,b,rnorm(1,0,0.0001)), "ish"))
},
`(` = function(a){
a*rnorm(1,1,0.001)
},
`<-` = function(a,b){
library(tidyverse)
#devtools::install_github("AckerDWM/gg3D")
library("gg3D")
flights <- nycflights13::flights
flights %>%
drop_na() %>%
filter(dest %in% c("LAX", "SFO", "SMF")) %>%
ggplot(aes(x = hour, y = dep_delay, z = arr_delay, color = month, shape = origin)) +
axes_3D(theta = 280, phi = 20) +
#!/bin/bash
find . -size +100M | sed 's|^\./||g' | cat >> .gitignore; awk '!NF || !seen[$0]++' .gitignore
sort -u .gitignore > .gitignore_new && mv .gitignore_new .gitignore
# thanks to https://stackoverflow.com/questions/4035779/gitignore-by-file-size#comment67586426_5200267
# run this BEFORE you reinstall R/RStudio
old_packages <- list.files(.libPaths())
saveRDS(old_packages, "test_old_packages.rds")
# now you close RStudio and reinstall the new R and RStudio
# now you run THIS after you've installed R and RStudio anew
old_pkg_import <- readRDS("test_old_packages.rds")
pkgs_to_install <- unique(old_pkg_import)
@MCMaurer
MCMaurer / find_rows_with_combo_of_values.R
Created December 14, 2018 23:06
Find all the rows in a dataframe where a certain sequence appears at any point
result <- matrix(nrow = nrow(mtcars), ncol = ncol(mtcars)-1)
for (i in 1:32) {
result[i,] <- rollapply(as.character(mtcars[i,]), 2, identical, c("1", "0"))
}
result2 <- vector(length = nrow(mtcars))
for (i in 1:32) {
result2[i] <- TRUE %in% result[i,]
}