0

For example, if we calculate Pearson correlation and P-value of first two variables of data set mtcars, results are something like this:

Correlation value:

      mpg  disp    
mpg   1.00 -0.85 
disp -0.85  1.00  

P-value:

      mpg    disp   
mpg  0.0000 0.0000 
disp 0.0000 0.0000       

Instead of this, Is there any way to have results like this:

          Corr.   p-value
mp  mp    1.00    0.0000
mp  dip  -0.85    0.0000

I have more than 200 variable and want to generate results like this and then write those results on CSV using write.csv command. Thank you!

Khashi
  • 47
  • 1
  • 7
  • Can you show your code – akrun May 31 '20 at 19:02
  • That's what i am looking for. Common code for two variables is cor.test(mtcars$mpg, mtcars$disp, conf.level = 0.95). Howeever, I am looking for a code in which I can generate corr. and p-value of correlation of each variable and importantly results in four columns as I gave example above. Thanks – Khashi May 31 '20 at 19:09

1 Answers1

2

If we wanted pairwise, cor.test, use combn

out <- combn(mtcars, 2, FUN = function(x) 
    cor.test(x[[1]], x[[2]], conf.level = 0.95), simplify = FALSE)
names(out) <- combn(names(mtcars), 2, FUN = paste, collapse='_')

The output of corr.test is a list

str(out[[1]])
#List of 9
# $ statistic  : Named num -8.92
#  ..- attr(*, "names")= chr "t"
# $ parameter  : Named int 30
#  ..- attr(*, "names")= chr "df"
# $ p.value    : num 6.11e-10
# $ estimate   : Named num -0.852
#  ..- attr(*, "names")= chr "cor"
# $ null.value : Named num 0
#  ..- attr(*, "names")= chr "correlation"
# $ alternative: chr "two.sided"
# $ method     : chr "Pearson's product-moment correlation"
# $ data.name  : chr "x[[1]] and x[[2]]"
# $ conf.int   : num [1:2] -0.926 -0.716
#  ..- attr(*, "conf.level")= num 0.95

It can be directly extracted with list extraction methods i.e. $ or [[

mydata <– do.call(rbind, Map(cbind, corgroups = names(out), 
 unname(lapply(out, function(x)
        data.frame(cor.value = x$estimate, cor.pvalue = x$p.value)))))
Khashi
  • 47
  • 1
  • 7
akrun
  • 874,273
  • 37
  • 540
  • 662
  • This command looks useful for correlation and p-value of all variables. However, after running this, we get everything in Console instead of Environment. Is there any command that gives us results of corr. value and p-value in the form of a data frame/ table in environment that can be further exported from R using write.csv command. Hope you understand my question. – Khashi May 31 '20 at 19:20