Correlation matrix with dplyr, tidyverse and broom - P-value matrix

ぃ、小莉子 提交于 2019-12-06 04:11:36

This answer is based on akrun's comment from this post. By using the rcorr function, we can calculate the correlations and P values. To access these components, use ds_cor$r and ds_cor$P.

set.seed(1164)

library(tidyverse)
library(Hmisc)

ds <- data.frame(id=(1) ,a=rnorm(10,2,1), b=rnorm(10,3,2), c=rnorm(5,1,05))

ds_cor <- ds %>%
  select(-id) %>% 
  as.matrix() %>%
  rcorr(type = "spearman")

ds_cor
#    a     b     c
# a  1.00  0.28 -0.42
# b  0.28  1.00 -0.25
# c -0.42 -0.25  1.00
# 
# n= 10 
# 
# 
# P
#   a      b      c     
# a        0.4250 0.2287
# b 0.4250        0.4929
# c 0.2287 0.4929       
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!