编写“ .rtest”使用R程序(ex)通过write.table输出到文件? [英] writing a ".rtest" output to file, using the R program (ex) via write.table?
问题描述
我正在使用R以特定的成对方式打开一些保存的.csv文件,并执行统计测试( mantel.rtest
,可在软件包 ade4中找到)。 .csv文件顺序命名为 fileAX或 fileBY,其中X和Y是整数。
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**
我试图使用各种功能(例如 unlist和 as.vector)将 mantelout转换为更友好的格式,但无济于事。有什么想法吗?
I tried to convert "mantelout" to some friendlier format using various functions such as "unlist" and "as.vector", to no avail. Any thoughts?
谢谢,
WAW
Thanks, WAW
编辑:我应该注意,此输出R环境中的测试如下:
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"
推荐答案
您可以使用 capture.output
+ writeLines
组合:
You could 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")
在您的情况下,应为:
writeLines(capture.output(mantelout), file = "C:\\results\\mantelout")
这篇关于编写“ .rtest”使用R程序(ex)通过write.table输出到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!