# Correlation matrix within a data.table

Thanks for the complete code. It would be slightly more useful as a `reprex`. Please see the FAQ: What's a reproducible example (`reprex`) and how do I do one? Using a reprex, complete with representative data will attract quicker and more answers.

``````library(data.table)
library(ellipse)
#>
#> Attaching package: 'ellipse'
#> The following object is masked from 'package:graphics':
#>
#>     pairs
library(magrittr) # for pipe
set.seed(955)
vvar <- 1:20 + rnorm(20,sd=3)
wvar <- 1:20 + rnorm(20,sd=5)
xvar <- 20:1 + rnorm(20,sd=3)
yvar <- (1:20)/2 + rnorm(20, sd=10)
zvar <- rnorm(20, sd=6)

## A data frame with multiple variables
data <- data.table(vvar, wvar, xvar, yvar, zvar)
# print(data,topn = 3)
## Make the correlation table
ctab <- data[,cor(.SD)] #data[,x := .(list(cor(.SD))),]
# print(ctab,topn = 3)
# plotcorr(cor(data), mar = c(0.1, 0.1, 0.1, 0.1))
# getting rounding
A <- as.matrix(ctab, rownames = TRUE) %>% round(.,2) %>% as.data.table(.,keep.rownames = TRUE) -> B
``````
``````# HOWEVER
``````
``````A == B
#>        rn vvar wvar xvar yvar zvar
#> [1,] TRUE TRUE TRUE TRUE TRUE TRUE
#> [2,] TRUE TRUE TRUE TRUE TRUE TRUE
#> [3,] TRUE TRUE TRUE TRUE TRUE TRUE
#> [4,] TRUE TRUE TRUE TRUE TRUE TRUE
#> [5,] TRUE TRUE TRUE TRUE TRUE TRUE
``````
``````# Doesn't seem as though there is anything to be gained by the round trip to B
``````
``````A <- as.matrix(ctab, rownames = TRUE) %>% round(.,2) -> A
``````
``````
# A <- round(A,2)
# B <-as.data.table(A,keep.rownames=TRUE)
# plotcorr(A, mar = c(0.1, 0.1, 0.1, 0.1))
# B[,plotcorr(as.matrix(.SD[2:6],rownames = TRUE), mar = c(0.1, 0.1, 0.1, 0.1)),]
# Do the same, but with colors corresponding to value
# colorfun <- colorRamp(c("#CC0000","white","#3366CC"), space="Lab")
# B[,plotcorr(as.matrix(.SD[2:6],rownames = TRUE),
#           col=rgb(colorfun((ctab+1)/2), maxColorValue=255),
#            mar = c(0.1, 0.1, 0.1, 0.1)),]
``````

Created on 2020-03-29 by the reprex package (v0.3.0)