hachiko
hachiko

Reputation: 747

R round correlate function from corrr package

I'm creating a correlation table using the correlate function in the corrr package. Here is my code and a screenshot of the output.

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

enter image description here

I think this would look better and be easier to read if I could round off the values in the correlation table. I tried this code:

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

But I get this error:

Error in Math.data.frame(list(term = c("prof_rank_factor", "yrs.since.phd", : non-numeric variable(s) in data frame: term

The non-numeric variables part of this error message doesn't make sense to me. When I look at the structure I only see integer or numeric variable types.

'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 ...

How can I clean up this correlation table with rounded values?

Upvotes: 1

Views: 416

Answers (2)

akrun
akrun

Reputation: 887108

After returning the tibble output with correlate, loop across the columns that are numeric and round

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

Upvotes: 3

TarJae
TarJae

Reputation: 78927

We can use:

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

Upvotes: 1

Related Questions