简体   繁体   English

R-将矩阵写入带有标题和脚注的输出文件

[英]R - Writing a Matrix to an Output File With a Title and Footnote

Currently I am doing a very custom job in R where I am populating a big matrix with values that are computed before. 目前,我在R中做一个非常自定义的工作,我在其中用以前计算的值填充一个大矩阵。 I send the matrix to an external file with the following: 我使用以下命令将矩阵发送到外部文件:

write.csv(m1, file = "OutputToExcel1.csv")

... where m1 is my matrix of interest and I already set an appropriate working directory. ...其中m1是我感兴趣的矩阵,并且我已经设置了适当的工作目录。

I would like to give it a descriptive title and possibly a footnote. 我想给它一个描述性的标题,也可以给它一个脚注。 In searching for a solution there appear to be many commands besides write.csv. 在寻找解决方案时,除了write.csv之外似乎还有许多命令。

Can anyone recommend an easy & standard & easy way to do this besides sink()? 除了sink()之外,有人可以推荐一种简便,标准和简便的方法吗?

If you don't want to use sink() , you can use file() connection and write.csv() between your writeLines() statements, but it looks like you have to wrap write.csv() with capture.output() , which is very similar to sink() . 如果您不想使用sink() ,则可以在writeLines()语句之间使用file()连接和write.csv() ,但是看起来您必须将write.csv()capture.output() ,这与sink()非常相似。

Example data: 示例数据:

# matrix for reproducible example
data("Harman74.cor")

dest_file <- "OutputToExcel1.csv"

The trick seems to be to use file(..., open='at') to support multiple appends: 窍门似乎是使用file(..., open='at')支持多个追加:

if(file.exists(dest_file)) file.remove(dest_file) # need empty file before append

fileConn<-file(dest_file, open='at')
write("Descriptive Title", file = fileConn)
write( capture.output(write.csv(Harman74.cor)), 
       file = fileConn, 
       append=TRUE )
write("Helpful footnote", file = fileConn)
close(fileConn)

file.show(dest_file)

If you're not completely set against using sink() , it is pretty straightforward. 如果您不完全反对使用sink() ,那将非常简单。 Taking advantage of the default behavior of write.csv() , which prints to console, you can just redirect this output. 利用可打印到控制台的默认行为write.csv() ,您可以重定向此输出。

sink(dest_file)
writeLines("Descriptive Title")
cat(write.csv(Harman74.cor))
writeLines("Helpful footnote")
sink()

file.show(dest_file)

The following seems to work, but it: 以下内容似乎可行,但它可以:

  • Puts an x in the upper-left of the spreadsheet 在电子表格的左上方放置一个x
  • Gives the number 1 before the title, & 在标题前加上数字1,
  • Puts the title in quotes. 将标题放在引号中。

I think the following might be "good enough?" 我认为以下内容可能“足够好?”

setwd("Y:/Zach/")
myfile<-"DSummary.csv"
write.table("Title", myfile)
write.table(m1, myfile, sep=",", col.names=FALSE, append=TRUE)

Below is a more elaborate stating of the above code: 以下是上述代码的详细说明:

setwd("Y:/Zach/")
myfile<-"DSummary.csv"
write.table("Title1: n = ", myfile)
write.table(SchoolCnt, myfile, col.names=FALSE, append=TRUE)
write.table(". Is good enough", col.names=FALSE, append=TRUE)
write.table(m1, myfile, sep=",", col.names=FALSE, append=TRUE)

So the way I am breaking up standard text strings and variables is very sloppy. 因此,我分解标准文本字符串和变量的方式非常草率。 Any suggestions on how I may concatenate them would be awesome! 关于如何连接它们的任何建议都很棒!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM