Skip to content

Instantly share code, notes, and snippets.

View jennybc's full-sized avatar

Jennifer (Jenny) Bryan jennybc

View GitHub Profile
@jennybc
jennybc / vanishingFactorLevels.r
Created April 11, 2014 04:36
Explore unusual behaviour of a factor returned by melt.data.frame
library(testthat)
library(reshape2) # installed from github
# SHA 096afb80192f163ced5d001cc416acc1d9a85d12
## working with example from test-melt.r line 132 -->
df <- data.frame(
id=1:2,
f1=factor(c("a", "b")),
f2=factor(c("b", "a"))
@jennybc
jennybc / much_country.r
Created May 13, 2014 21:38
Writes tabular data and a figure for each country in the Gapminder excerpt
library(plyr)
library(ggplot2)
gDat <- read.delim("gapminderDataFiveYear.txt")
d_ply(gDat, ~ country, function(z) {
the_country <- z$country[1]
the_continent <- z$continent[1]
the_filename <- paste0(the_continent, "_", the_country, ".tsv")
@jennybc
jennybc / 2014-05-26_totals-in-df-tabulation.R
Last active August 29, 2015 14:01
Get multi-factor tabulation into a data.frame AND append a row for a grand total
library(plyr)
## what's the best way to get tabulation of (days31, oystersOK) into a
## data.frame AND get a grand total row reflecting the expected total of 12
## months?
## related discussion has occured before here:
## http://stackoverflow.com/questions/4946873/how-do-i-add-a-row-to-a-data-frame-with-totals
## related issue open on dplyr:
@jennybc
jennybc / 2014-05-27_dplyr-does-not-like-AsIs.r
Last active August 29, 2015 14:01
Demonstrate dplyr::group_by() error when a variable has class 'AsIs'
library(dplyr)
## accept default behavior, i.e. stringsAsFactors = TRUE
## downside is that 'name' really shouldn't be a factor
df <- data.frame(name = c("jenny", "jim", "reed", "lorrie"),
gender = c("female", "male", "male", "female"))
df %>% group_by(gender) %>% summarize(count = n()) # this works
## protect a single character variable with I()
df <- data.frame(name = I(c("jenny", "jim", "reed", "lorrie")),
---
title: "Wrapper function to open/close graphics device"
author: "Jenny Bryan"
date: "25 July, 2014"
output:
html_document:
keep_md: TRUE
---
```{r setup, include = FALSE, cache = FALSE}
@jennybc
jennybc / knit-with-errors.rmd
Created September 8, 2014 06:53
Make knitr tolerate errors (snippet for reference)
```{r setup, include = FALSE, cache = FALSE}
knitr::opts_chunk$set(error = TRUE)
```
@jennybc
jennybc / 2014-09-18_numeric-index-gotcha.md
Last active August 29, 2015 14:06
How quickly indexing with numeric can burn you

Inspired by recent @johnmyleswhite tweets on indexing (though they focus more on NA stuff)

My standard advice to R novices is "don't worry about numeric vs integer too much, for now", but I just created an indexing example that scares me.

I did not realize it was sooo easy to create this gotcha when indexing with numeric (which I know is not a great idea).

@jennybc
jennybc / 2014-09-29_knitr-treatment-errors.md
Created September 30, 2014 04:35
2014-09-29_knitr-treatment-errors

Demo errors in knitted doc vs. other

Jenny Bryan
29 September, 2014

Context: In a recent lesson, I claim (with some justification) that raising an error inside a function via if() ... stop() often gives more info than using stopifnot(). This is partly because you get to write your own error message. But also because you learn the name of the function that threw the error. Or so I thought.

Here's a toy function to generate an error.

@jennybc
jennybc / 2014-11-05_dplyr-join-reprex.md
Created November 5, 2014 18:44
Demonstrate that dplyr::left_join() can mangle factor levels
suppressPackageStartupMessages(library(dplyr))

Get word counts for Character * Chapter in LOTR trilogy

if(!file.exists("lotr_clean.tsv")) {
  download.file(paste0("https://raw.githubusercontent.com/jennybc/",
 "lotr/master/lotr_clean.tsv"), 
@jennybc
jennybc / digest_header_links.R
Created February 7, 2015 19:50
Digest link header in paginated GitHub API request