LTribe
LTribe

Reputation: 37

Reformatting Results of cor.test to dataframe

I am struggling with reformatting the results of a cor.test() to a data.frame. The Results are formatted as a List but can't simply be written into a dataframe because unlist() doesn't seem to understand items of type "double".

The Results look like this: Results cor.test()

Now I would like to reformat the results of multiple cor.tests into a dataframe with data.name as columnnames, and all other list items in individual rows. This should then look something like this:

                 data.name1      data.name2     
   statistic     5.390954        xxxxxxxxx
   p.value       7.00845e-08     xxxxxxxxx
   estimate      0.1612965       xxxxxxxxx
   null.value    0               xxxxxxxxx
   alternative   two.sided       xxxxxxxxx
   method        kendall         xxxxxxxxx

Sorry for this Beginner's Question but I am really struggeling to get this somehow and already researched for hours. I am very grateful for any help.

Upvotes: 0

Views: 522

Answers (1)

Axeman
Axeman

Reputation: 35262

A tidy approach:

library(broom)
library(purrr)

l <- list(
  c1 = cor.test( ~ hp + qsec, mtcars),
  c2 = cor.test( ~ hp + mpg, mtcars)
)

map_dfr(l, tidy, .id = 'id')
# A tibble: 2 x 9
  id    estimate statistic p.value parameter conf.low conf.high method
  <chr>    <dbl>     <dbl>   <dbl>     <int>    <dbl>     <dbl> <chr> 
1 c1      -0.708     -5.49 5.77e-6        30   -0.848    -0.477 Pears~
2 c2      -0.776     -6.74 1.79e-7        30   -0.885    -0.586 Pears~
# ... with 1 more variable: alternative <chr>

In your example output, everything will have to be coerced to characters, which is not a very nice way to store numeric outcomes such as regression coefficients and p-values.

Upvotes: 1

Related Questions