Writing the ".rtest" output to a file using the R (ex) program via write.table?

I use R to open some saved CSV files in a specific way and perform a statistical test ( mantel.rtest found in ade4 package). .Csv files are sequentially called either "fileAX" or "fileBY", where X and Y are integers .

I want to save the results of this test in a single file, but I have some problems.

Here is the code (please forgive the inefficient use of "paste":

 library(ade4) x <- 1:15; y <- 1:15 filename1 <- paste(paste(c("fileA"), 1:15, sep = ""), ".csv", sep = "") filename2 <- paste(paste(c("fileB"), 1:15, sep = ""), ".csv", sep = "") for (i in seq(along=x)) { M1 <- read.table(paste("C:\\scripts\\", filename1[i], sep = ""), header = FALSE, sep = ",") for (j in seq(along=y)) { M2 <- read.table(paste("C:\\scripts\\", filename2[j], sep = ""), header = FALSE, sep = ",") mantelout <- mantel.rtest(dist(matrix(M1, 9, 9)), dist(matrix(M2, 9, 9)), nrepet = 99) write.table(mantelout, file = "C:\\results\\mantelout") } } 

Attempting to do this results in the following error message:

 **Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class '"rtest"' into a data.frame** 

I tried to convert "mantelout" to some friendlier format using various functions like "unlist" and "as.vector", but to no avail. Any thoughts?

Thanks WAW

EDIT: It should be noted that the result of this test in the R environment is as follows:

 Monte-Carlo test Observation: 0.5324712 Call: mantel.rtest(m1 = dist(matrix(M1, 9, 9)), m2 = dist(matrix(M2, 9, 9)), nrepet = 99) Based on 99 replicates Simulated p-value: 0.01" 
0
source share
2 answers

Use str(rtest) to look at the structure of the rtest object: it is not surprising that it will not fit in data.frame. Try putting it on a list. You can save the list as a file using save(my.list, file="mylist.RData") and reload it later using load("mylist.RData") .

0
source

You can use capture.output + writeLines combo:

 # example from help("lm") ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) group <- gl(2,10,20, labels=c("Ctl","Trt")) weight <- c(ctl, trt) lm.D9 <- lm(weight ~ group) writeLines(capture.output(lm.D9), file="my analysis.txt") 

In your case should be:

 writeLines(capture.output(mantelout), file = "C:\\results\\mantelout") 
0
source

All Articles