This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I have a vector of strings that map to known numeric values. | |
# What's the cleanest/most reader-friendly R idiom for this conversion? | |
# sample data | |
df = expand.grid( | |
x_str = c("string1", "secondstring", "blah", "garbagestring"), | |
replicate=1:3, | |
stringsAsFactors=FALSE) | |
# Approach 1: Encode the look-up table as its own dataframe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Demo of intermittent plotting failures. | |
# Apparent requirements: | |
# * Interactive session (I'm in R.app using Quartz graphics) | |
# * Arranging multiple figures using gridExtra::arrangeGrob | |
# * Very different figure complexities | |
# (i.e one plot big enough to have a rendering lag, one that renders much faster.) | |
# * No graphics device open at beginning of arrangeGrob call | |
# Gentle reader, can you reproduce this on your machine? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
gather = tidyr::gather | |
df = data.frame( | |
one=c(1,NA,NA,3, NA), | |
two=c(NA,3,NA,4,NA), | |
three=c(NA,NA,2,NA,5), | |
x=rnorm(5)) | |
result = (df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library("tidyr") | |
raw = read.csv("the_data.csv", header=FALSE, colClasses="character") | |
colnames(raw) = (data.frame(r1=unlist(raw[1,]), r2=unlist(raw[2,])) | |
%>% mutate(r1=sub("^$", NA, r1)) | |
%>% fill(r1) | |
%>% transmute( | |
colname=paste(r1,r2, sep=".")) | |
%>% unlist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Stack two PNGs on top of each other, | |
# filling any leftover pixels with white | |
# | |
# N.B. I'm assuming the alpha channel is always 1; | |
# I *think* this will handle transparency with no changes, but haven't tested | |
library("png") | |
# these are plain R arrays with 3 dimensions (row, col, rgba) | |
# -- not even wrapped in a class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = data.frame( | |
date=as.Date(c("2016-07-31", "2016-08-01", "2016-08-03", "2016-08-04", "2016-08-10")), | |
x=rnorm(5)) | |
# Origin to use might be OS-specific? | |
num2date = function(x){as.character(as.Date(x, origin="1970-01-01"))} | |
(ggplot(a, aes(as.numeric(date), x)) | |
+geom_point() | |
+scale_x_reverse(labels=num2date) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#--- | |
#name: test | |
#fields: | |
# - name: site | |
# units: character | |
# description: The place we did the stuff | |
# - name: date | |
# units: Date | |
# range: {min: 2010-01-22, max: 2016-12-31} | |
# required: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I have a set of taxonomies and I want to return a consensus taxonomy masking the levels that aren't shared by all members, e.g. | |
["Liliopsida;Poales;Poaceae;Elymus;Elymus repens", "Liliopsida;Poales;Poaceae;Elymus;Elymus nutans"] | |
should produce "Liliopsida;Poales;Poaceae;Elymus;", | |
but those plus "Liliopsida;Poales;Cyperaceae;Carex;Carex ovalis" should equal "Liliopsida;Poales;;;" | |
My first stab: | |
def mask_to_agreement(taxstrings): | |
taxlists = [t.split(';') for t in taxstrings] | |
taxout = [''] * len(taxlists[0]) # assumes all lists are same length! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
badly_behaved_function = function(x)if(x==1){data.frame(a=1:5, b=5:1)}else{list()} | |
obtained = data.frame(id=1:3) %>% group_by(id) %>% do(res = badly_behaved_function(.$id)) | |
obtained | |
# Source: local data frame [3 x 2] | |
# Groups: <by row> | |
# | |
# # A tibble: 3 × 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(testthat) | |
options(tdtest=NULL) | |
test_that("options revert", { | |
op=options() | |
options(tdtest="testing") | |
on.exit(options(op)) | |
}) | |
getOption("tdtest") | |
# [1] "testing" |