Created
March 29, 2016 17:50
-
-
Save MichaelChirico/01b0039e049e8a5d024760e6028c62b1 to your computer and use it in GitHub Desktop.
cor from matrix data.frame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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