Skip to content

Instantly share code, notes, and snippets.

Avatar

Hadley Wickham hadley

View GitHub Profile
View gist:10238
month_seq <- seq(as.Date("2000-01-01"), as.Date("2000-12-31"), "month")
months <- factor(months(month_seq), levels = months(month_seq))
months_abbr <- factor(months(month_seq, TRUE), levels = months(month_seq, TRUE))
wday_seq <- seq(as.Date("2000-01-02"), as.Date("2000-01-08"), "day")
wdays <- factor(weekdays(wday_seq), levels = weekdays(wday_seq))
wdays_abbr <- factor(weekdays(wday_seq, TRUE), levels = weekdays(wday_seq, TRUE))
second <- function(x) as.POSIXlt(x)$sec
minute <- function(x) as.POSIXlt(x)$min
View gist:74686
measure <- structure(list(age = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("7-yrs", "11-yrs",
"15-yrs", "21-yrs"), class = "factor"), trt = structure(c(2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("alternation",
"repetition"), class = "factor"), value = c(814, 500, 424, 394,
967, 574, 472, 446, 10, 6, 6, 4, 13, 7, 6, 3), variable = c("rt",
"rt", "rt", "rt", "rt", "rt", "rt", "rt", "error", "error", "error",
"error", "error", "error", "error", "error")), .Names = c("age",
"trt", "value", "variable"), row.names = c(NA, 16L), class = "data.frame")
plot <- ggplot(measure, aes(age, value, colour = trt, fill = trt)) +
View dist.r
# dhist.
# An another algorithm for computing histogram breaks. Produces irregular bins.
# Provided by Lorraine Denby
#
#
# @keyword internal
dhist <- function(x, a=5*diff(quantile(x, c(0.25,0.75))), nbins=10, rx = range(x)) {
x <- sort(x)
if(a == 0)
a <- diff(range(x))/100000000
View points.r
mid_range <- function(x) mean(range(x, na.rm = TRUE))
centres <- ddply(county_df, c("state", "county"), summarise,
lat = mid_range(lat),
long = mid_range(long)
)
bubbles <- merge(centres, unemp, by = c("state", "county"))
ggplot(bubbles, aes(long, lat)) +
geom_polygon(aes(group = group), data = state_df,
colour = "white", fill = NA) +
@hadley
hadley / reproducible.md
Created Jan 6, 2010
How to write a reproducible example
View reproducible.md

How to write a reproducible example.

You are most likely to get good help with your R problem if you provide a reproducible example. A reproducible example allows someone else to recreate your problem by just copying and pasting R code.

There are four things you need to include to make your example reproducible: required packages, data, code, and a description of your R environment.

  • Packages should be loaded at the top of the script, so it's easy to see which ones the example needs.

  • The easiest way to include data in an email is to use dput() to generate

View helpr.r
library(sinartra)
source("memoise.r")
source("help-parse-rd.r")
source("help-package.r")
source("help-topic.r")
source("help-index.r")
router <- Router$clone()
View clustergram-had.r
ks.default <- function(rows) seq(2, max(3, rows %/% 4))
many_kmeans <- function(x, ks = ks.default(nrow(x)), ...) {
ldply(seq_along(ks), function(i) {
cl <- kmeans(x, centers = ks[i], ...)
data.frame(obs = seq_len(nrow(x)), i = i, k = ks[i], cluster = cl$cluster)
})
}
all_hclust <- function(x, ks = ks.default(nrow(x)), point.dist = "euclidean", cluster.dist = "ward") {
View coord-munch.r
#' For munching, only grobs are lines and polygons: everything else is
#' transfomed into those special cases by the geom.
#' @examples
#' nz <- data.frame(map("nz", plot=FALSE)[c("x","y")])
#' munch_data(nz, segment_length = 0.1)
munch_data <- function(data, segment_length) {
data <- add_group(data)
n <- nrow(data)
aes_df <- data[setdiff(names(data), c("x", "y"))]
View dendro.r
data <- iris
d <- dist(scale(iris[, 1:4]))
h <- hclust(d, "ward")
data$ORDER <- order(h$order)
data$HEIGHT <- 0
data$LEVEL <- 0
data$POINTS <- 1
@hadley
hadley / gist:837414
Created Feb 21, 2011
which-encoding.r
View gist:837414
all <- iconvlist()
names(all) <- all
tries <- llply(all[330:350], function(cur) iconv(song$artist, cur, "UTF-8"))
vapply(tries, function(x) sum(is.na(x)), integer(1))