Skip to content

Instantly share code, notes, and snippets.

@MichaelChirico
Created March 29, 2016 17:50
Show Gist options
  • Save MichaelChirico/01b0039e049e8a5d024760e6028c62b1 to your computer and use it in GitHub Desktop.
Save MichaelChirico/01b0039e049e8a5d024760e6028c62b1 to your computer and use it in GitHub Desktop.
cor from matrix data.frame
library(data.table)
DT <- as.data.table(ChickWeight)[ , lapply(.SD, as.numeric)]
DF <- as.data.frame(cor(DT))
#^ here's what you have from Stata^
#convert to matrix
M <- as.matrix(DF)
#axe the lower triangle
M[lower.tri(M, diag = TRUE)] <- NA
#now to data.table w/ melt:
DT <- as.data.table(M, keep.rownames = TRUE)
melt(DT, id.vars="rn")[!is.na(value)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment