[英]writing a “.rtest” output to file, using the R program (ex) via write.table?
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"). 我正在使用R以特定的成对方式打开一些保存的.csv文件并执行统计测试(
mantel.rtest
,在软件包“ ade4”中找到)。 The .csv files are sequentially named as either "fileAX" or "fileBY", where X and Y are integers. .csv文件顺序命名为“ fileAX”或“ fileBY”,其中X和Y是整数。
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. 我试图使用各种功能(例如“ unlist”和“ as.vector”)将“ mantelout”转换为更友好的格式,但无济于事。 Any thoughts?
有什么想法吗?
Thanks, WAW 谢谢,哇
EDIT: I should note that the output of this test in the R environment looks as follows: 编辑:我应该注意在R环境中此测试的输出如下所示:
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"
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. 使用
str(rtest)
来查看rtest
对象的结构:不足为奇的是它不会容纳在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")
. 您可以使用
save(my.list, file="mylist.RData")
将列表另存为文件,并稍后使用load("mylist.RData")
重新加载load("mylist.RData")
。
You could use capture.output
+ writeLines
combo: 您可以使用
capture.output
+ writeLines
组合:
# 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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.