dplyr or reshape help with "merging" lists from correlation matrix output

59 views
Skip to first unread message

Zachary Pierce

unread,
May 5, 2016, 12:44:53 PM5/5/16
to Davis R Users' Group
Hi Users
I am working with correlations from rcorr (Hmisc pack.).  I feed in my csv, and conduct my correlation test.  rcorr outputs 3 lists, containing spearman's rho values, n, and p values, like this:
corr15=read.csv("phylum_week15_corr.csv", row.names = 1)
l2=(rcorr(as.matrix(corr15, type=c("spearman"))))
as.table(round(l2$r,3))

        MPO15  NEO15  W15PU  W15P2  W15PV  W15P4  W15P5  W15P6  W15P7  W15P8  W15P9 W15P10 W15P11 W15P12 W15P13 W15P14 W15P15 W15P16 
MPO15   1.000  0.211  0.076  0.047  0.068         0.049  0.101  0.009 -0.028  0.008        -0.099 -0.054         0.014  0.027 -0.032        -0.034 -0.011 -0.050 -0.070
NEO15   0.211  1.000  0.090  0.007 -0.032.....

I can't seem to convert the entire output to a data frame or table, as I would like to create  a single "matrix" or table that contains the r, n and p values for each pairwise comparison, like:

                 MPO6               NEO6                          W6PU
MPO6 r 1 0.193 0.13
  p 0 0.0013 0.0306
NEO6 r 0.193 1 -0.021
p 0.0013 0 0.7235

Currently, I'm only aware of how to write a .csv from each list, then merge those in excel.  

Any thoughts?  Good explanations of commands and functions would be very helpful as I'm still learning how these things work.  Oh and by the way, what would be absolutely terrific is if one could figure out how to extract only the first two rows (MPO15, NEO15) as those contain all the meaningful pairwise comparisons.

I'm attaching my dataset if that helps.  Forgive the poor copying and posting above.

Thanks a million,
Zach 
phylum_week15_corr.csv

Brandon Hurr

unread,
May 5, 2016, 1:28:04 PM5/5/16
to davi...@googlegroups.com
I think I might have got it... Check it?

# first I extract the data from the matrices
#then make it long form
# then add a column of type
pgather <-
data.frame(rnames = rownames(l2$P), round(l2$P,3) ) %>%
gather(., names, value, -rnames) %>%
mutate(type = "p")

rgather <-
data.frame(rnames = rownames(l2$r), round(l2$r,3) ) %>%
gather(., names, value, -rnames) %>%
mutate(type = "r")
ngather <-
data.frame(rnames = rownames(l2$n), round(l2$n,3) ) %>%
gather(., names, value, -rnames) %>%
mutate(type = "n")

#then we put everything in a single dataframe
# and spread it back out by names

bind_rows(pgather, rgather, ngather) %>%
spread(., names, value)

HTH,
B


--
Check out our R resources at http://d-rug.github.io/
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
Visit this group at https://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.

Brandon Hurr

unread,
May 5, 2016, 1:33:59 PM5/5/16
to davi...@googlegroups.com
library(tidyr)
library(dplyr)

Also library(Hmisc) for rcorr() in your original post. 

Zachary Pierce

unread,
May 9, 2016, 12:27:49 AM5/9/16
to Davis R Users' Group
Perfect!  I will learn these functions.

Thanks a million Brandon!!!

Matt Espe

unread,
May 9, 2016, 1:23:38 PM5/9/16
to Davis R Users' Group
Hi,

There is a far simpler approach: 

## Label the row names on each table so you can tell them apart later
ends
= names(l2)
for(i in seq_along(l2)){
row
.names(l2[[i]])<-paste(row.names(l2[[i]]), ":", ends[i])
}

## You can use rbind since each list has the same column structure
result
<- do.call(rbind, l2)


Reply all
Reply to author
Forward
0 new messages