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 / 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 / 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;%

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 / 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)
@aammd
aammd / catcol.R
Last active December 29, 2015 11:19
a demostration of using catcolwise -- a function that creates a function.
## make a fake data frame
test <- data.frame(foo=gl(n=3,k=2,labels=c("low","med","high")),bar=rep(letters[1:2],3),zoop=runif(6))
## these are all factors
str(test)
## I want them to be characters, not factors, but I am too lazy to run as.character on every categorical factor
library(plyr)
test_char <- catcolwise(as.character)(test)
@aammd
aammd / eps.R
Created August 22, 2013 21:26
Writing publication-pretty *.eps files in R
eps <- function(file, onefile=FALSE, width=5.5, height = 5.5, horizontal=FALSE,paper="special", ...){
postscript(file=file,width=width,height=height,onefile=onefile,horizontal=horizontal,paper=paper,title=file,...)
par(bty='l')
}
@aammd
aammd / local_maxima.md
Created August 19, 2013 23:08
a friend and I worked out a simple means to detect local maxima of a mixed frequency distribution -- useful for determining if populations have diverged or not!
x1 <- rnorm(100,5,sd=2)
x2 <- rnorm(200,15,3)
x3 <- c(x1,x2)

## calculate the density function 
den_x <- density(x3)]
## this can be plotted:
plot(den_x)
getmatch <<- function(x,str2match,...) {
unlist(regmatches(x,regexpr(str2match,x,...))) }
@aammd
aammd / refexp.r
Created July 24, 2013 18:14 — forked from emhart/refexp.r
library(lme4)
library(ggplot2)
#create some levels
levs <- as.factor(c("l1","l2","l3","l4","l5"))
#set the factor means
f_means <- c(6,16,2,10,13)
# set individual as a factor
## file-scan.r
## Function to find a pattern in files with user-defined extension (.r by default)
## Returns file name and approximate row matches if positive.
## -------------------------------------------------------
## Author: Laura Tremblay-Boyer ([email protected])
## Written on: March 26, 2013
## Time-stamp: <2013-03-26 16:19:28 Laura>
file.scan <- function(pattern, dir=getwd(), fpattern=NA, ftype=".r") {