0

我正在运行此代码:

max.print <- getOption('max.print')
options(max.print=nrow(countryaccepted) * ncol(countryaccepted))
sink(file.txt, append=TRUE, type="out")
cat("*************************\n")
cat("F-Test and T-Test Results")
as.array(HypothesisTesting)
cat("\n\n\n")
sink()
options(max.print=max.print)

变量“HypothesisTesting”是一个 3D 数组,尺寸为 2 x 2 x 2,包含“double”类型的值。

当我通过“源”运行代码时,我只在文件中得到以下结果

*************************
F-Test and T-Test Results

但是当我在“控制台”中运行它时,我得到以下结果保存在文件中:

*************************
F-Test and T-Test Results

, , TTest

         H0 Accepted H0 Rejected
Ho True     98.68938    0.970427
H0 False     8.62801    1.310620

, , FTest

         H0 Accepted H0 Rejected
Ho True     100.0000     4.22076
H0 False     7.50504     0.00000

为什么结果没有通过源保存,为什么只通过控制台保存?

我哪里错了?

4

1 回答 1

0

print()如果要通过函数或源保存对象的(控制台)输出,则 需要使用。

*************************
F-Test and T-Test Results
, , 1

     [,1] [,2]
[1,] 0.01 0.01
[2,] 0.01 0.01

, , 2

     [,1] [,2]
[1,] 0.01 0.01
[2,] 0.01 0.01

代码

arr <- array(0.01, dim=c(2, 2, 2))

max.print <- getOption('max.print')
options(max.print = 100)

sink("file.txt", append = TRUE, type = "out")
cat("*************************\n")
cat("F-Test and T-Test Results\n")
print(as.array(arr))                                   # wrap output into print()
cat("\n\n\n")
sink()
options(max.print = max.print)
于 2018-10-31T11:54:33.550 回答