Skip to content

Instantly share code, notes, and snippets.

@karawoo
karawoo / Warp equivalent
Last active August 29, 2015 14:08
Convert warp speed to meters/second
warp_equivalent <- function(warpfactor, originalseries = FALSE) {
if (originalseries == TRUE) {
v <- (warpfactor^3) * 299792458 # speed of light -- 299792458 m/s
} else if (originalseries == FALSE) {
if (warpfactor > 9.0 & warpfactor <= 10.0) {
x <- -0.5 * log10(10 - warpfactor)
} else {
x <- 0
}
v <- (warpfactor^((10/3) + x)) * 299792458
# minimal dataset
dat <- data.frame(a = c(1, 5, 2),
b = c(5, 2, 9),
c = c(4, 3, 1))
rowMeans(dat)
# [1] 3.333333 3.333333 4.000000
do.call(rbind, lapply(seq_len(nrow(dat)), function (k) {mean(as.numeric(dat[k, ]))}))
library("plyr")
library("dplyr")
### create some sample data
set.seed(20)
dat <- data.frame(lake = c(rep("Muddy", 6), rep("Clear", 6)),
host = c(rep(c("a", "b"), 6)),
var = sample(c(0, 1, 2), 12, replace = TRUE),
stringsAsFactors = FALSE)

Where I live there are a lot of little drive-thru coffee shops that like to advertise special beverages. Many of these disturb me. Here is a non-comprehensive list of ones I have seen:

  • Starburst Red Bull
  • Red Bull huckleberry lemonade
  • Rainbow sherbet Red Bull bombers
  • Almond Joy mocha
  • Pineapple upside down Red Bull
  • Banana bread latte
  • Pecan pie latte
  • Butterfinger mocha
@karawoo
karawoo / server.R
Created July 31, 2015 21:34
Toggling user-selected layers with ggplot2 in a Shiny app
library("shiny")
library("ggplot2")
shinyServer(function(input, output) {
output$myplot <- renderPlot({
## Create a boxplot with optional jittering
p <- ggplot(iris, aes(x = Species, y = Petal.Length)) +
# In response to: https://twitter.com/duffy_ma/status/628300075110936576
df <- data.frame(x=1:10, y = 1:10)
library("plyr")
df$y <- mapvalues(df$y, from = c(5), to = c(NA)) # make sure to keep `y` column numeric
lengthdata <- length(df$x)
for(i in 1:(lengthdata-1)) {
if (is.na(df$y[i])) { # use is.na to test for NAs
@karawoo
karawoo / stacked_area.R
Created February 20, 2016 21:53
Stacked area charts
library("ggplot2")
library("dplyr")
###########################################################
#### This does not work to create stacked area chart ####
###########################################################
## Create data
set.seed(40)
dat <- data.frame(x = rep(c(1:10), 3),
## Load ggplot package
library("ggplot2")
## Create sample data
dat <- structure(list(x = c(2.69753522456158, 9.77329733015504,
0.579014160879888, 6.01934352936223,
5.53224114519544, 7.52123757028021, 1.1418573057279,
5.81543114883825, 1.98635061332025,
8.98725295660552),
y = c(1.1341254551895, 8.69686821205542, 1.0851298507303,
@karawoo
karawoo / data.csv
Last active June 29, 2016 20:19
Reading in CSVs with headers on multiple rows
B0S2 B0S3 B0S4
abundance percent cover height abundance percent cover height abundance percent cover height
5 50 71 8 30 9 45
5 3 47 1 1
## Load tidyverse package, which contains ggplot2, dplyr, tidyr, etc.
library("tidyverse")
## Create some data
set.seed(432)
dat <- tibble(fert = c(rnorm(n = 10, mean = 0.6, sd = 0.2),
rnorm(n = 10, mean = 0.4, sd = 0.2)),
treat = rep(c("25-25", "28-28"), each = 10))
## Plot boxplots with points on top + yellow X for mean & legend