Skip to content

Instantly share code, notes, and snippets.

View padpadpadpad's full-sized avatar
🙃

Daniel Padfield padpadpadpad

🙃
View GitHub Profile
@padpadpadpad
padpadpadpad / quick_data_wrangling.R
Last active March 12, 2019 14:37
example of re-scaling some questionnaire data for Sarah
# load packages
library(tidyr)
library(dplyr)
# load in data
d <- read.csv('~/Desktop/orig_ratings.csv', stringsAsFactors = FALSE)
# stack participants
d_stack <- gather(d, 'participant', 'score', 2:ncol(d))
@padpadpadpad
padpadpadpad / double_x_axis_ggplot2.R
Last active February 11, 2019 16:40
makes a multi-row x axis on ggplot2.
# load packages
library(ggplot2)
library(dplyr)
library(tidyr)
# create dummy data
df=data.frame(PM=as.factor(1:8),
phase=rep(c('1', '2'),each = 4),
y=rnorm(8, mean = 10, sd = 3))
@padpadpadpad
padpadpadpad / cat_help_symbols_to_nums.R
Last active February 1, 2019 14:17
Random script converting symbols and numbers to just numbers
# load package
library(dplyr)
library(tidyr)
# read in data
d <- readRDS('~/Desktop/subs_GUD')
glimpse(d)
# need to group by tray1,day, site, trayno, type
@padpadpadpad
padpadpadpad / label_facets_ggplot2.R
Last active March 28, 2019 07:49
function to label facets with letters in ggplot2
# load package
library(ggplot2)
# write function
label_facets <- function(string){
len <- length(string)
string = paste('(', letters[1:len], ') ', string, sep = '')
return(string)
}
@padpadpadpad
padpadpadpad / seglm_through_origin.R
Last active December 14, 2018 11:11
forcing a segmented regression through the origin
# forcing a segmented regression through the origin.
# load packages
library(segmented)
# make data
d <- data.frame(x = c(3, 13, 18, 19, 19, 26, 26, 33, 40, 49, 51, 53, 67, 70, 88
),
y = c(3.56211608128595, 10.5214485148819, 3.66063708049802, 6.11000808621074,
# quick linear regression using broom and the tidyverse ####
# load packages
library(tidyr)
library(ggplot2)
library(dplyr)
library(broom)
library(gapminder)
library(janitor)
library(purrr)
@padpadpadpad
padpadpadpad / quick_decay_script.R
Last active September 5, 2018 13:44
Implement a simple decay model
# load packages
library(ggplot2)
library(broom)
# load in data
d <- read.csv('where/your/data/is.csv', stringsAsFactors = FALSE)
# try and fit an nls model using the exponential decay model
N(t) = N0 * e^-a*t
# N0 = number at t 0. here t is FiO2
@padpadpadpad
padpadpadpad / segmented_lm_vs_lm.R
Last active August 3, 2018 09:07
code for cat
# example for Cat
# set seed
set.seed(42)
# load packages ####
library(ggplot2)
library(dplyr)
library(tidyr)
library(broom)
@padpadpadpad
padpadpadpad / lin_segment.R
Last active July 26, 2018 12:55
script to run a breakpoint analysis where the second slope is constrained to 0
# want to model the data as a segmented regression, so the curve has two straight lines, segmented regression can do this
# xChange is the x value at which the gradients change when the gradient after xChange is forced to be 0
lin_segment <- function(a, x, xChange, c){
lin.segment <- a*(abs(x - xChange) - (x + xChange)) + c
return(lin.segment)
}
@padpadpadpad
padpadpadpad / stock_sol_vol2.R
Last active November 8, 2018 10:53
function for working out dilutions for stock solutions
# stock sol vol 2
# stock_sol_conc is the concentration of the stock solution - current OD
# new_sol_conc is the concentration of the new solution - the desired OD
# diluent vol is the volume of the diluent you want to use (i.e. the amount of water/M9)
stock_sol_vol2 <- function (stock_sol_conc, new_sol_conc, diluent_vol)
{
return((new_sol_conc * diluent_vol)/(stock_sol_conc - new_sol_conc))
}