如何在R中的.txt输出的标题中添加描述行 [英] How to add description lines to the header of a .txt output in R
本文介绍了如何在R中的.txt输出的标题中添加描述行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从 data.frame
对象创建一个.csv或.txt文件,该对象还包含一些描述变量详细信息的行。
I'm trying to create a .csv or .txt file from a data.frame
object that also contains some lines describing more detail about the variables.
这是我的第一个尝试:
Head <- "
#variables:
#sal - Salinity [PSU]
#temp - Temperature [degrees Celsius]
"
n <- 10
df <- data.frame(sal=runif(n, 30, 37), temp=runif(n, 15, 17))
df
sink("data.txt")
Head
df
sink()
导致这个:
[1] "\n#variables [units]:\n#sal - Salinity [PSU]\n#temp - Temperature [degrees Celcius]\n"
sal temp
1 32.11494 15.35176
2 30.57537 16.80972
3 32.90651 15.95174
4 30.62192 15.73436
5 31.43069 15.45873
6 34.38173 15.69713
7 31.27954 15.01126
8 32.77093 16.22493
9 35.99510 15.10123
10 35.52409 15.49084
但是,我真的很喜欢这样:
but, I would really like it to look like this:
#variables [units]:
#sal - Salinity [PSU]
#temp - Temperature [degrees Celcius]
sal temp
1 32.11494 15.35176
2 30.57537 16.80972
3 32.90651 15.95174
4 30.62192 15.73436
5 31.43069 15.45873
6 34.38173 15.69713
7 31.27954 15.01126
8 32.77093 16.22493
9 35.99510 15.10123
10 35.52409 15.49084
推荐答案
使用 cat
而不是让R调用对象的打印
方法。
Use cat
instead of letting R call the object's print
method.
sink("data.txt")
cat(Head)
df
sink()
这篇关于如何在R中的.txt输出的标题中添加描述行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文