Skip to content

Instantly share code, notes, and snippets.

View hadley's full-sized avatar

Hadley Wickham hadley

View GitHub Profile
# lazy data frame
#
# like idata.frame, but exploits lazy evaluation + macro code generation
# instead of active bindings
library(vadr)
lazy.frame <- function(df, enclos=parent.frame(), ...) UseMethod("lazy.frame")
lazy.frame.lazy.frame <- function(df, ...) df
@hadley
hadley / server.R
Created December 23, 2013 21:52 — forked from anonymous/server.R
# server.R
library("shiny")
shinyServer(
function(input, output){
output$displayCounter <- renderText({
c("value of counter:", updateCounter())
})
vectorised_r <- function(x, y) {
exp(-abs(x - y))
}
devectorised_r <- function(x, y) {
r <- rep(NA, length(x))
for(i in seq_along(x)) {
r[i] <- exp(-abs(x[i] - y[i]))
}
r
@hadley
hadley / latex-math.r
Last active December 17, 2015 08:09 — forked from crowding/latex-math.r
# User facing function
#
# to_math(x[1] + 1^{2 + 4} + 5 + sqrt(y) / 5 %/% 10)
# to_math(paste(x^2, y - 1, z_i))
# to_math(hat(tilde(ring(x))))
# to_math(pi*r^2)
# to_math(unknown_call(x, floor(sqrt(z))))
# to_math(u1(x) + u2(x))
to_math <- function(x) {
x <- substitute(x)

Load Data With Hmisc

Often times when working with data in R, you find yourself needing to summarise a data.frame by a column, a SELECT SUM(..) or SELECT COUNT(..) in MySQL if you will.

For instance, you may have a lot of page-visits to analyse and you aren't quite certain how to summarise some of that data using R.

Let's consider the following (trivial) dataset: