Skip to content

Instantly share code, notes, and snippets.

@pedroj
Created March 25, 2013 16:37
Show Gist options
  • Save pedroj/5238521 to your computer and use it in GitHub Desktop.
Save pedroj/5238521 to your computer and use it in GitHub Desktop.
vectorize
# STACK: Turn table (C) into (D):
# C
# a b c d
# A 3 2 . .
# B . . 1 1
# D
# A a 3
# A b 2
# A c .
# A d .
# B a .
# B b .
# B c 1
# B d 1
#-------------------------------------------------------------
vectorize <- function(mat)
{
mat <- t(mat)
cbind(expand.grid(dimnames(mat))[2:1], as.vector(mat))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment