1

我有这个代码:

data(iris)
for (i in 1:5){
  max.print <- getOption('max.print')
  options(max.print=nrow(iris[1:4,]) * ncol(iris[1:4,]))
  sink('dframe.txt',append=TRUE)
  iris[1:4,]
  sink()
  options(max.print=max.print)

  write('                       ', file='dframe.txt',append=TRUE)
  write('###############',file='dframe.txt',append=TRUE)

}

我的目标是在 for 循环中填充一个 .txt 文件。但是,当我看到输出文件dframe.txt时,它看起来像:

###############

###############                       

###############                       

############### 

###############                       

###############

然而,当我手动运行时,我可以看到预期的输出被写入了 5 次:

Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa

我该如何解决这个问题?我正在使用append=TRUE,但显然还不够。

4

0 回答 0