Appending information lines to the beginning of a data.frame when writing to txt in R

不打扰是莪最后的温柔 提交于 2019-12-07 17:53:31

You can make this work by using commas (for example) instead of whitespace to separate the data columns. You'll of course then need to specify the sep="," argument to both write.table() and read.table().

(Incidentally, the extra control provided by the many possible arguments to write.table() is one reason to generally prefer write.table(df, ..., append=TRUE) over sink(fn); df; sink(). With sink(), the data.frame gets written to a file in same way it would be printed to the console, giving you much less control over details of its representation.)

fn <- "data.txt"
writeLines(Head, fn)
write.table(df, fn, append=TRUE, quote=TRUE, sep=",")

## Reading data from the file now works fine 
dd <- read.table(fn, header=TRUE, sep=",")
head(dd, 4)
#        sal     temp            datetime
# 1 35.28238 16.48981 2001-01-01 10:00:01
# 2 31.80891 16.68704 2001-01-01 10:00:01
# 3 32.22510 15.87365 2001-01-01 10:00:01
# 4 33.13408 16.60193 2001-01-01 10:00:01
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!