[R] How can output tables be converted to data files?

David L Carlson dcarlson at tamu.edu
Wed Apr 5 23:49:10 CEST 2017


Data files is pretty vague. If you save the output of a function such as rcorr(), you can extract any of the parts you need. Step 1 is to read the documentation:

?rcorr

Under the section labeled "Value" you will see that rcorr() returns a list with 3 matrices named r, n, and P:

> Y <- rcorr(as.matrix(X))
> str(Y)
List of 3
 $ r: num [1:3, 1:3] 1 -0.934 -0.814 -0.934 1 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:3] "mpg" "cyl" "disp"
  .. ..$ : chr [1:3] "mpg" "cyl" "disp"
 $ n: int [1:3, 1:3] 5 5 5 5 5 5 5 5 5
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:3] "mpg" "cyl" "disp"
  .. ..$ : chr [1:3] "mpg" "cyl" "disp"
 $ P: num [1:3, 1:3] NA 0.0201 0.0937 0.0201 NA ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:3] "mpg" "cyl" "disp"
  .. ..$ : chr [1:3] "mpg" "cyl" "disp"
 - attr(*, "class")= chr "rcorr"
> Y$r
            mpg        cyl       disp
mpg   1.0000000 -0.9341083 -0.8138289
cyl  -0.9341083  1.0000000  0.9342174
disp -0.8138289  0.9342174  1.0000000
> Y$n
     mpg cyl disp
mpg    5   5    5
cyl    5   5    5
disp   5   5    5
> Y$P
            mpg        cyl       disp
mpg          NA 0.02010207 0.09368854
cyl  0.02010207         NA 0.02005248
disp 0.09368854 0.02005248         NA

You can save the whole list with save() or write individual matrices as .csv files with write.csv().

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352


-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of BR_email
Sent: Wednesday, April 5, 2017 4:09 PM
To: r-help at r-project.org
Subject: [R] How can output tables be converted to data files?

Hi R'ers:
This code produces: 3x3 rcorr matrix, one-element vector, and 3x3 
p-value matrix.
I would like to use these outputs as data files.
How can these output tables be converted to data files?
Any assistance is appreciated.
Thanks. Bruce

library(Hmisc)
mtcars5 <- mtcars[sample(1:nrow(mtcars), 5, replace=FALSE),]
X<- as.matrix(mtcars5[, c(1,2,3)])
print(X)
rcorr(as.matrix(X))

--

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.



More information about the R-help mailing list