[R] write.table read.table with Dates

Don MacQueen macq at llnl.gov
Thu Nov 10 19:44:33 CET 2005


In addition to the solutions already provided, note that if *all* you 
want to do is save your dataframe in a file, and later recreate it 
from that file, you can use dump().

dump('Tall',file='Tall.r')
rm(Tall)  ## just to demonstrate that the next command will recreate Tall
source('Tall.r')

-Don

At 2:21 PM +0100 11/10/05, JeeBee wrote:
>I've found several similar issues with write.table/read.table
>with Dates on this list, but trying to follow this advice I still
>get an error.
>
>First, I read in data from several files, constructing several date/time
>columns using ISOdatetime
>
>>  str(Tall$Begin)
>'POSIXct', format: chr [1:40114] "2005-10-02 00:00:00" "2005-10-02
>00:00:00" ...
>>  length(Tall$Begin)
>[1] 40114
>>  class(Tall$Begin)
>[1] "POSIXt"  "POSIXct"
>
>This looks good (time is not always 00:00:00 ...)
>This data came from several files, now I want to store the result I have
>in data.frame Tall and be able to retrieve this quickly some other time.
>
>This is what I do:
>write.table(Tall, file="somefile.csv", sep=",", qmethod="double",
>row.names=FALSE)
>
>Later, I do this to read the file again:
>fieldnames=c("Begin","test-a","test-b","Eind")
>T=read.table(file = "somefile.csv", col.names = fieldnames,
>   header = TRUE, sep = ",", quote="\"", fill=FALSE)
>
>I understand T$Begin now is a factor. I tried to simply convert it
>again using (as I read on this mailinglist ...):
>Q = strptime(as.character(T$Begin),format="%Y-%m-%d %H:%M:%S")
>
>Q is looking good, though its length I don't understand .. is it a list or
>something? It seems there are 40114 values in there somewhere...
>
>>  class(Q)
>[1] "POSIXt"  "POSIXlt"
>>  length(Q)
>[1] 9
>>  str(Q)
>'POSIXlt', format: chr [1:40114] "2005-10-02 00:00:00" "2005-10-02 
>00:00:00" ...
>
>T$Begin = Q ### yields this error
>Error in "$<-.data.frame"(`*tmp*`, "Begin", value = list(sec = c(0, 0,  :
>         replacement has 9 rows, data has 40114
>
>Could somebody explain me how to convert the date column?
>Or perhaps there is an easier way?
>
>Thanks in advance for your time.
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


-- 
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA




More information about the R-help mailing list