LN3
LN3

Reputation: 67

How to put correlation results into a table in R?

I have some correlation calculations I'm running between columns in a dataframe like this:

cor(df$length, df$col1, method = c("pearson"), use = "complete.obs")
cor(df$length, df$col1, method = c("spearman"), use = "complete.obs")

cor(df$length, df$col2, method = c("pearson"), use = "complete.obs")
cor(df$length, df$col2, method = c("spearman"), use = "complete.obs")

cor(df$length, df$col3, method = c("pearson"), use = "complete.obs")
cor(df$length, df$col3, method = c("spearman"), use = "complete.obs")

I'm trying to figure out how to get these results into their own table, giving a table like:

Col   Pearsons  Spearman
Col1   0.1       0.2
Col2   0.003     0.5
Col3   0.6       0.9

I've been trying to alter code from a similar question which is:

result <- do.call(rbind, by(df, df$length, FUN = function(x) {
  tmp <- cor.test(x$Col1, x$length, method = "spearman")
}))

But this doesn't look right and I'm not sure how to compress my correlation code in to table - what functions can I use to do this with my correlation code?

Example input data:

df <- structure(list(length = c(144001L, 1731L, 337L), col1 = c(3L, 
3L, 4L), col2 = c(8L, 2L, 6L), col3 = c(18L, 
1L, 1L)), row.names = c(NA, -3L), class = c("data.table", "data.frame"
))

Upvotes: 1

Views: 868

Answers (2)

akrun
akrun

Reputation: 887118

We can use tidyverse

library(dplyr)
library(tidyr)
df %>%
  summarise(across(starts_with('col'), ~ 
    list(tibble(Pearsons = cor(length, ., method = 'pearson',
            use = 'complete.obs'),
      Spearman = cor(length,., method = "spearman", use = 'complete.obs'))))) %>% 
 pivot_longer(cols = everything()) %>% 
 unnest(c(value))

-output

# A tibble: 3 x 3
  name  Pearsons Spearman
  <chr>    <dbl>    <dbl>
1 col1    -0.507   -0.866
2 col2     0.750    0.5  
3 col3     1.00     0.866

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388982

You can do this with apply family of functions.

t(sapply(df[, -1], function(x) {
  c(Pearsons = cor(df$length, x, method = "pearson", use = "complete.obs"), 
    Spearman = cor(df$length, x, method = "spearman", use = "complete.obs"))
}))

#       Pearsons   Spearman
#col1 -0.5072948 -0.8660254
#col2  0.7503742  0.5000000
#col3  0.9999643  0.8660254

Upvotes: 1

Related Questions