来自 corrr 包的 R 轮相关函数

R round correlate function from corrr package

我正在使用 corrr 包中的相关函数创建关联 table。这是我的代码和输出的屏幕截图。

correlation_table <- corrr::correlate(salary_professor_dataset_cor_table, 
                                      method = "pearson")
correlation_table

我认为如果我可以对相关性 table 中的值进行四舍五入,这样看起来会更好并且更容易阅读。我试过这段代码:

correlation_table <- round(corrr::correlate(salary_professor_dataset_cor_table, 
                                      method = "pearson"),2)

但是我得到这个错误:

Math.data.frame(list(term = c("prof_rank_factor", "yrs.since.phd", 中的错误:数据框中的非数字变量:term

此错误消息的非数字变量部分对我来说没有意义。当我查看结构时,我只看到整数或数字变量类型。

'data.frame':   397 obs. of  6 variables:
 $ prof_rank_factor : num  3 3 1 3 3 2 3 3 3 3 ...
 $ yrs.since.phd    : int  19 20 4 45 40 6 30 45 21 18 ...
 $ yrs.service      : int  18 16 3 39 41 6 23 45 20 18 ...
 $ salary           : num  139750 173200 79750 115000 141500 ...
 $ sex_factor       : num  1 1 1 1 1 1 1 1 1 2 ...
 $ discipline_factor: num  2 2 2 2 2 2 2 2 2 2 ...

如何用四舍五入的值清除这种相关性 table?

我们可以使用:

options(digits=2)
correlation_table <- corrr::correlate(salary_professor_dataset_cor_table, 
                                      method = "pearson")
correlation_table

在使用 correlate 返回 tibble 输出后,循环 across numericround

的列
library(dplyr)
corrr::correlate(salary_professor_dataset_cor_table, 
                                  method = "pearson") %>%
    mutate(across(where(is.numeric), round, digits = 2))