In R, is it possible to find multiple correlation matrix?
Any package/in-built function available?
Is it possible to specify order of correlations in cor()?
In R, is it possible to find multiple correlation matrix?
Any package/in-built function available?
Is it possible to specify order of correlations in cor()?
The base R function cor()
produces a multiple correlation matrix.
The order of variables in its output will mimic their order in the data frame to which you apply it, so arrange your data frame, then apply cor()
. Here's an example using piping in dplyr
so you don't actually modify the stored data frame:
df <- data.frame(x = seq(10), y = rev(seq(10)), z = seq(10))
df %>%
select(z, y, x) %>% # Use select() to reorder variables in df
cor(.)
Result:
z y x
z 1 -1 1
y -1 1 -1
x 1 -1 1