Hello, it will be possible to simplify, improve the following lines of code:
library(data.table)
library(ellipse)
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))),]
#ctab <-ctab[,x[1:20]]
#without the desired result !!!!!
#!!!ctab<-ctab[,lapply(.SD, round, digits = 2),by=.SD[1,],.SDcols = 1:5]
#!!!ctab<-ctab[,lapply(.SD, round, digits = 2),by=.SD[1,],.SDcols = 1:5]
#!!!ctab[,.SD[1,], round(.SD[, 1:5], 2)]
#without the desired result !!!!!
print(ctab,topn = 3)
plotcorr(cor(data), mar = c(0.1, 0.1, 0.1, 0.1))
A<-as.matrix(ctab, rownames = TRUE)
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)),]
a) Especially step A <-as.matrix (ctab, rownames = TRUE) and then perform the inversion B <-as.data.table (A, keep.rownames = TRUE)
to be able to manipulate the plotcorr function inside the data.table
b) how to achieve the round on the data.table ctab immediately after the ctab <- data [, cor (.SD)]