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

∥☆過路亽.° 提交于 2019-12-10 11:43:55

问题


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 POSIXltclass, 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.

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.

Examples:

###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

回答1:


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


来源:https://stackoverflow.com/questions/21764361/appending-information-lines-to-the-beginning-of-a-data-frame-when-writing-to-txt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!