Skip to content

Instantly share code, notes, and snippets.

View aammd's full-sized avatar

Andrew MacDonald aammd

  • Université de Sherbrooke
  • Montreal, Canada
View GitHub Profile
@aammd
aammd / ordered.R
Created December 3, 2013 22:42
A demonstration of how you can use ordered contrasts when your hypothesis involves the an *ordinal* response to factor levels. i.e. when your factor levels can be placed in a sequence based on their predicted effect: low<med<high
## a demonstration of ordered contrasts
## three levels, A, B, and C
factor1 <- rep(LETTERS[1:3],c(15,15,15))
## Another factor
factor2 <- rep(letters[1:3],15)
## check combinations
table(factor1,factor2)

Simple data manipulations in R

Many years ago, I was introduced to R by Cam Webb . At the time, his website contained a list of common data manipulations (original here). This list dated from Cam's early experience with R, and contained the R-help mailing list responses to a series of data manipulations. For a long time, I kept this file as a handy reference. I printed it out. I recommended it to friends.

Now I have been using R for years, and the state of the art has advanced considerably. Particulary, Hadley Wickham's reshape2 and dplyr packages have transformed the way most useRs manipulate their data. I decided that it would be interesting to revisit my favourite resource and try my hand at solving these problems with tools from these two packages.

library(reshape2)
library(dplyr)
@aammd
aammd / fizzbuzz.md
Last active January 25, 2017 08:25
An example of calculating "FizzBuzz" in R using dplyr and magrittr

Apparently, there is a simple problem called Fizz buzz which is sometimes used to identify competent programmers. A good opportunity to practice some dplyr and magrittr tricks.

library(dplyr)
library(magrittr)
library(knitr)

1:100 %>%
 data.frame %&gt;%
@aammd
aammd / ListToDf.md
Last active March 26, 2024 23:31
turning a named list into a dataframe using dplyr

Is there an easy way to convert a named list into a dataframe, preserving the elements of the list in a "list-column"?

    library(dplyr)
    library(magrittr)

    ## make a random matrix
    rand_mat <- function() {
@aammd
aammd / boot.R
Created June 12, 2014 21:08 — forked from davharris/boot.R
set.seed(1)
# Create fake data
x = runif(100, 0, 5)
y = .25 * x^3 - x^2 + rnorm(length(x))
data = data.frame(x = x, y = y)
# Identify 500 points to include in the plots
x.sequence = seq(0, 5, length = 500)
@aammd
aammd / boot_reg.md
Last active August 31, 2017 03:12
bootstrapping regressions with `dplyr`.

Bootstrap confidence intervals for linear and nonlinear models

Another version of this gist with figures included is on Rpubs

Recently I was trying to put confidence intervals on a regression line, and I got some excellent advice from @davidjayharris on Twitter, who suggested the below method in an excellent

On Rosetta Code, the list of Tasks uncompleted in R contains the following challenge: print the Bernoulli numbers from B0 to B60. After seeing some solutions, including one in in Python , I wanted to try in R. There are examples of code on that website, and a nice description of the algorithm here.

 library(MASS)
## via base ####
"%gcd%" <- function(u, v) {ifelse(u %% v != 0, v %gcd% (u%%v), v)}
n <- 20L
js <- 0:n
Jp1 <- js + 1L
Ns <- matrix(NA,nrow = n + 1, ncol = n + 1)
Ds <- Ns
Ns[1,] <- 1L
## 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())
}
values <- c(2,5,3,6,7,
9,5,4,9,9,
1,5,4,8,1,
3,1,5,6,2,
2,9,4,7,4)
my.mat <- matrix(values, nrow = 5, byrow = TRUE)
library(dplyr)
library(tidyr)