Skip to content

Instantly share code, notes, and snippets.

View jennybc's full-sized avatar

Jennifer (Jenny) Bryan jennybc

View GitHub Profile
@jennybc
jennybc / jenny01.r
Created October 19, 2012 18:38
Learning where to put your .Rprofile, where your R library is
## this is where your .Rprofile should live
R.home(component = "home")
## this is R's main library for add-on packages
.Library
## this shows all the libraries you have
## for newbies, will look same as above
.libPaths()
@jennybc
jennybc / toyline.R
Created September 11, 2013 07:09
Toy example used in STAT 545A
a <- 2
b <- 7
sigSq <- 0.5
n <- 400
set.seed(1234)
x <- runif(n)
y <- a + b * x + rnorm(n, sd = sqrt(sigSq))
(avgX <- mean(x))
@jennybc
jennybc / reorderWeirdness.r
Last active December 23, 2015 13:49
Minimal example showing strange behaviour of reorder.factor in gdata when R script is processed with knitr, but not when run in Console
## Minimal example demonstrating Warning fiasco re: `reorder.factor()` from gdata
## verify a fresh clean search path
search()
## tiny excerpt from some Gapminder data I teach with
foo <- data.frame(country = c("China", "Cuba", "Gambia", "Mali", "Norway"),
continent = c("Asia", "Americas", "Africa", "Africa", "Europe"),
intercept = c(47.2, 62.2, 28.4, 33.1, 72.2),
slope = c(0.53, 0.32, 0.58, 0.38, 0.13))
@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")),
@jennybc
jennybc / 2014-07-18_lazy-evaluation-hangnail.md
Last active October 26, 2017 18:04
What R's lazy evaluation does and does not imply

Lazy evaluation hang nail

Jenny Bryan
18 July, 2014

A group of us at UBC are working through Wickham's Advanced R Programming book together. We just tackled the chapter on Functions, which reminded me of this ...

In the subsection on "Default and missing arguments", we have: "Since arguments in R are evaluated lazily (more on that below), the default value can be defined in terms of other arguments:"

---
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 / twee-demo.Rmd
Last active August 14, 2022 21:50
twee(): emulating the tree directory listing command
---
title: "twee demo"
author: "Jenny Bryan"
date: "17 August, 2014"
output:
html_document:
toc: TRUE
keep_md: TRUE
---