在R中写入txt时将信息行附加到data.frame的开头 [英] Appending information lines to the beginning of a data.frame when writing to txt in R
问题描述
我刚刚收到了关于在.txt输出开头添加文本行的上一个问题的一些很好的建议。这对我的示例数据效果很好,但是我现在意识到,我的实际数据有一个 POSIXlt
类的变量,它包含了日期和时间值之间的空格(例如 2001-01-01 10:00:01)。这似乎是导致R的问题,以了解他们的数据列数。对于上一个问题,我已经尝试了两种建议,但似乎没有任何效果。我甚至尝试写作.csv文件来更好地定义分隔,但这也失败了。
I just received some great advice on a previous question regarding appending text lines to the beginning of a .txt output. This worked well for my example data, but I now realize that my actual data has a variable of the POSIXlt
class, which contains a space between the day and hour values (e.g. "2001-01-01 10:00:01"). This seems to be causing problems for R to understand how many columns of data their are. I have tried variations on both suggestions given to the previous question, but nothing seems to work. I even tried writing as a .csv file to better define the separations, but this also failed.
任何帮助将不胜感激。我可能在这里做一些非正统的事情吗?我应该单独制作一个readme.txt文件来包含变量描述并避免所有的这种挫败感吗?我希望数据集对于未来的用户是合乎逻辑的,不言而喻的。
Any help would be greatly appreciated. Am I perhaps doing something unorthodox here? Should I just make a separate "readme.txt" file to contain the variable descriptions and avoid all of this frustration? I want the data sets to be logical and self-explanatory to future users.
###Example dataset
Head <-
"#variables:
#sal - Salinity [PSU]
#temp - Temperature [degrees Celsius]
#datetime - Date [yyyy-mm-dd hh:mm:ss]
"
n <- 10
df <- data.frame(sal=runif(n, 30, 37), temp=runif(n, 15, 17), datetime=as.POSIXlt("2001-01-01 10:00:01"))
df
###Create .txt (or .csv?)
#option 1
fn <- "data.txt"
sink(fn)
cat(Head)
df
sink()
read.table(fn)
#Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
# line 1 did not have 5 elements
#option 2
fn <- "data.txt"
writeLines(Head, fn)
write.table(df, fn, append=TRUE, quote=FALSE)
#Warning message:
#In write.table(df, fn, append = TRUE, quote = FALSE) :
# appending column names to file
read.table(fn)
#Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
# line 1 did not have 5 elements
#option 3
fn <- "data.csv"
sink(fn)
cat(Head)
write.csv(df)
sink()
read.csv(fn, header=TRUE)
#Error in read.table(file = file, header = header, sep = sep, quote = quote, :
# more columns than column names
推荐答案
您可以使用逗号(例如)而不是空格分隔数据列。你当然需要将 sep =,
参数指定为 write.table()
和 read.table()
。
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()
.
(顺便提一句,额外的控件由许多可能的参数提供到 write.table()
是通常喜欢 write.table(df,...,append = TRUE)
超过
sink(fn); df; sink()
使用 sink()
,data.frame以相同的方式写入文件,将其打印到控制台,减少对其表示细节的控制。)
(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
这篇关于在R中写入txt时将信息行附加到data.frame的开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!