[R] Correlation Matrix in R

William Revelle lists at revelle.net
Tue Nov 1 15:56:12 CET 2011


Alexandre,
  The output from corr.test is a list of matrices.  To export one of those matrices, simply specify which one you want:

Using the example from my previous note:

> library(psych)
> examp <- corr.test(sat.act)
> mat.c.p <- lower.tri(examp$r)*examp$r + t(lower.tri(examp$p)*examp$p)
> mat.c.p

mat.cp is a matrix and can be directly written using write.table (if you want).

To find out what are the elements of the list produced by corr.test, use the str command;

str(examp)  will produce

List of 5
 $ r   : num [1:6, 1:6] 1 0.0873 -0.0209 -0.0365 -0.0188 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:6] "gender" "education" "age" "ACT" ...
  .. ..$ : chr [1:6] "gender" "education" "age" "ACT" ...
 $ n   : num [1:6, 1:6] 700 700 700 700 700 687 700 700 700 700 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:6] "gender" "education" "age" "ACT" ...
  .. ..$ : chr [1:6] "gender" "education" "age" "ACT" ...
 $ t   : num [1:6, 1:6] Inf 2.314 -0.551 -0.965 -0.498 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:6] "gender" "education" "age" "ACT" ...
  .. ..$ : chr [1:6] "gender" "education" "age" "ACT" ...
 $ p   : num [1:6, 1:6] 0 0.0209 0.5818 0.3349 0.6187 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:6] "gender" "education" "age" "ACT" ...
  .. ..$ : chr [1:6] "gender" "education" "age" "ACT" ...
 $ Call: language corr.test(x = sat.act)
 - attr(*, "class")= chr [1:2] "psych" "corr.test"


To export one of those as a text file you could just copy the output, or you can write.table one element.  e.g.,

write.table(examp$r)

Bill

On Nov 1, 2011, at 4:43 AM, AlexC wrote:

> Hello, 
> 
> Thank you for your replies.  I cannot run the function rcor.test even when
> having loaded package ltm.  Perhaps it has to do with the fact that I am
> using the latest version of R and this package wasn't created under that
> version
> 
> The function corr.test in package psych works fine.  Is there anyway to
> export the results in a txt or csv file?  Since it isn't in a data frame
> format it cannot simply be exported using write.table
> 
> Alexandre 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/Correlation-Matrix-in-R-tp3938274p3962939.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 

William Revelle		           http://personality-project.org/revelle.html
Professor			           http://personality-project.org
Department of Psychology   http://www.wcas.northwestern.edu/psych/
Northwestern University	   http://www.northwestern.edu/
Use R for psychology             http://personality-project.org/r
It is 6 minutes to midnight	   http://www.thebulletin.org



More information about the R-help mailing list