Skip to content
Prev 396616 / 398502 Next

please help generate a square correlation matrix

?s 20:47 de 25/07/2024, Yuan Chun Ding escreveu:
Hello,

Here are two other ways.

The first is equivalent to your long format attempt.


library(tidyverse)

dat %>%
   names() %>%
   expand.grid(., .) %>%
   apply(1L, \(x) {
     tmp <- dat[rowSums(dat[x]) > 0, ]
     tmp2 <- cor.test(tmp[[ x[1L] ]], tmp[[ x[2L] ]])
     c(tmp2$estimate, P = tmp2$p.value)
   }) %>%
   t() %>%
   as.data.frame() %>%
   cbind(tmp_df, .) %>%
   na.omit()


The second is, in my opinion the one that makes more sense. If you see 
the results, cor is symmetric (as it should) so the calculations are 
repeated. If you only run the cor.tests on the combinations of 
names(dat) by groups of 2, it will save a lot of work. But the output is 
a much smaller a data.frame.


cbind(
   combn(names(dat), 2L) %>%
     t() %>%
     as.data.frame(),
   combn(dat, 2L, FUN = \(d) {
     d2 <- d[rowSums(d) > 0, ]
     tmp2 <- cor.test(d2[[1L]], d2[[2L]])
     c(tmp2$estimate, P = tmp2$p.value)
   }) %>% t()
) %>% na.omit()



Hope this helps,

Rui Barradas