简体   繁体   中英

Create a correlation matrix from a table with correlation coefficients

I have created a table of correlation coefficients that I'd like to modify to a correlation matrix -style table.

This is the style of my table of correlation coefficients. The correlations are between biological marker values and other variables:

table <- data.frame(marker = "a", "b", "c", "d",
                var1 = c(0.12, 0.33, -0.42, 0.51),
                var2 = c(-0.62, -0.28, 0.35, -0.42),
                var3 = c(0.22, -0.74, 0.10, 0.77),
                var4=c(0.05, -0.36, 0.66, 0.47))

I haven't figured out how to modify this kind of table to a "melted" correlation matrix format. The problem is in my table I have correlation coefficients calculated with different methods (Spearman's and tetrachoric), so I wasn't able to create a simple correlation matrix. Instead I've gathered this table of coefficients manually.

Any chance something like the following would work for you?

suggested_table <- data.frame(
matrix(
  c(
    "a", 0.12, -0.62, 0.22, 0.05,
    "b", 0.33, -0.28, -0.74, -0.36,
    "c", -0.42, 0.35, 0.10, 0.66,
    "d", 0.51, -0.42, 0.77, 0.47
  ),
  nrow = 4,
  ncol = 5,
  byrow = TRUE,
  dimnames = list(NULL,
                  c("marker", "a", "b", "c", "d"))
),
stringsAsFactors = FALSE
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM