0

I am using a piece of code suggested by the answer to this question:

How to calculate correlation by group

correlate <- d %>%
  group_by(Group, Sex, CS.NCS) %>% 
  summarise(cor.test(Corticosterone, Behaviour.Frequency, use = "pairwise.complete.obs"))


correlate
  cor.test(Corticosterone, Behaviour.Frequency, use = "pairwise.complete.obs")
1                                                                     1.404187
2                                                                           77
3                                                                     0.164284
4                                                                    0.1580116
5                                                                            0
6                                                                    two.sided
7                                         Pearson's product-moment correlation
8                                       Corticosterone and Behaviour.Frequency
9                                                      -0.06538313, 0.36632341

But how do I get more descriptive results, rather than just 1-9. I don't know what these numbers mean or what they refer to. How do I get this to tell me what I'm looking at?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
DS14
  • 133
  • 9

1 Answers1

0

You can try with nest_by from dplyr (>=1.0.0) and broom package:

library(dplyr)
library(broom)
d %>%
  nest_by(Group, Sex, CS.NCS) %>% 
  mutate(nc = list(cor.test(~Corticosterone+Behaviour.Frequency, data = data, use = "pairwise.complete.obs"))) %>% 
  summarise(broom::tidy(nc))

As an example:

> mtcars %>% 
    nest_by(cyl) %>% 
    mutate(nc = list(cor.test(~wt+qsec, data = data))) %>% 
    summarise(broom::tidy(nc))
`summarise()` regrouping output by 'cyl' (override with `.groups` argument)
# A tibble: 3 x 9
# Groups:   cyl [3]
    cyl estimate statistic p.value parameter conf.low conf.high method                               alternative
  <dbl>    <dbl>     <dbl>   <dbl>     <int>    <dbl>     <dbl> <chr>                                <chr>      
1     4    0.638      2.49  0.0347         9  0.0618      0.895 Pearson's product-moment correlation two.sided  
2     6    0.866      3.87  0.0117         5  0.325       0.980 Pearson's product-moment correlation two.sided  
3     8    0.537      2.20  0.0479        12  0.00834     0.831 Pearson's product-moment correlation two.sided 
iago
  • 2,990
  • 4
  • 21
  • 27