views:

41

answers:

1

Hi all,

I am using R to open up some saved .csv files in a particular pairwise manner and perform a statistical test ("mantel.rtest", found in the package "ade4"). The .csv files are sequentially named as either "fileAX" or "fileBY", where X and Y are integers.

I'd like to save the results of this test in a single file, but am running into some issues.

Here's the code (please forgive the inefficient usage 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 such as "unlist" and "as.vector", to no avail. Any thoughts?

Thanks, WAW

EDIT: I should note that the output of this test in the R environment looks 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"

A: 

Use str(rtest) to have a look at the structure of the rtest object: it's no surprise it won't fit in a data.frame. Try putting it in a list instead. You can save the list as a file using save(my.list, file="mylist.RData"), and reload it later using load("mylist.RData").

Michael Dunn
thanks again. is there an easy way to save the output (as in the information that is displayed in the R editor after "printing" the test results) as a text file?
Are you sure you need that? If so you might be better off constructing your own data.frame with the attributes of the rtest object you're interested in. But for viewing the results of the analysis the R objects are better, since the data underlying the summary is also available to you if you need it.
Michael Dunn
I don't know how to dump the screen output to a file in R, except by using unix redirection (and I gathered from the file paths in your other question you're using windows). It would make a good SO question...
Michael Dunn