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)) --
David L Carlson
2017-Apr-05 21:49 UTC
[R] How can output tables be converted to data files?
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$rmpg cyl disp mpg 1.0000000 -0.9341083 -0.8138289 cyl -0.9341083 1.0000000 0.9342174 disp -0.8138289 0.9342174 1.0000000> Y$nmpg cyl disp mpg 5 5 5 cyl 5 5 5 disp 5 5 5> Y$Pmpg 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.
Bruce Ratner PhD
2017-Apr-05 22:07 UTC
[R] How can output tables be converted to data files?
Thanks, David. ______________ Bruce Ratner PhD> On Apr 5, 2017, at 5:49 PM, David L Carlson <dcarlson at tamu.edu> wrote: > > 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. >
David: If I may, I was successful in writing csv files for rcorr, n, but not pvalue, which is empty. I set digits=5, thinking the small numbers might not show with default setting. Here's the code I used. Do you see a bug? Thanks. Bruce Y <- rcorr(as.matrix(X)) digits=5 write.csv(Y$p, file = "c:/R_data/pvalue.csv",row.names=FALSE, na="") - empty write.csv(Y$n, file = "c:/R_data/nsize.csv",row.names=FALSE, na="") - okay write.csv(Y$r, file = "c:/R_data/rcorr.csv",row.names=FALSE, na="") - okay David L Carlson wrote:> Y <- rcorr(as.matrix(X))