Skip to content

Instantly share code, notes, and snippets.

## Empty list -- just use the empty environment for this.
nil <- function() {
emptyenv()
}
## Test if a list is the empty list:
is_empty <- function(lis) {
identical(lis, nil())
}
# A quick function to save a PBM (http://en.wikipedia.org/wiki/Netpbm_format)
# visualize *a lot* of missing data pretty quickly (outside of R).
writeMissingPBM <- function(x, file) {
dims <- dim(x)
x[] <- as.integer(is.na(x))
con <- file(file, open="wt")
writeLines(sprintf("P1\n%d %d", ncol(x), nrow(x)), con)
write.table(x, file=con, sep=" ", col.names=FALSE, row.names=FALSE, quote=FALSE)
close(con)
@jennybc
jennybc / 2014-08-22_rbind-and-store-as-var.Rmd
Last active August 8, 2016 03:26
Row bind a list of data.frames with a key
---
title: "Row bind a list of data.frames with a key"
author: "Jenny Bryan"
date: "22 August, 2014"
output:
html_document:
keep_md: TRUE
---
I posed a question on Twitter (click to see the figure!):
@cscheid
cscheid / main.jl
Created September 10, 2014 22:51
bare-bones stress majorization in julia
# You should totally ignore this because it's the first piece of Julia I've ever written.
# 2-clause BSD, blah.
function make_cycle(n)
result = Dict{Int32, Array{Int32}}()
for i = 1:n
ii = i - 1
push!(result, i, [1 + ((i+n-2) % n), 1 + i % n])
end
result
# convert c("female", "Male") to c(0,1)
# magrittr is way nicer than nested function calls
convert_sexcodes <-
function(codes)
{
require(magrittr)
tolower(codes) %>%
@jennybc
jennybc / r-fcsn-in-wild-search.md
Last active January 6, 2020 08:09
Search for "natural" usage of a function across all CRAN packages

What if a function in a package has no examples? Or is poorly exampled? Wouldn't it be nice to find functioning instances of it "in the wild"?

Via Twitter, Noam Ross taught me a clever way to do such searches on GitHub. Put this into the GitHub search box to see people using the llply() function from plyr:

"llply" user:cran language:R

Or just click here.

@barryrowlingson
barryrowlingson / smarties.R
Created March 4, 2015 12:03
Smarties colour palette
### Colours of old Smarties with lovely artificial colouring, derived from:
###
###
### "Smarties old new" by John Penton and Paul Hughes - Own work. Licensed under CC BY-SA 2.5 via Wikimedia Commons - https://commons.wikimedia.org/wiki/File:Smarties_old_new.jpg#mediaviewer/File:Smarties_old_new.jpg
###
### image was loaded into gimp, representative parts of each Smartie were isolated, the image converted to indexed palette
### with 9 colours, the palette then saved.
##
### "Smarties" is a trademark of Nestle
##

Playing with adding + operator for easily joining geojson objects together into a single valid geojson object. This is relatively easy with lists, but not so easy with json unless you speak json.

Get lawn package too for viewing data

devtools::install_github("ropensci/geojsonio")