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.
On 11/10/05, JeeBee <JeeBee at troefpunt.nl> 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? >You are converting it to POSIXlt (which represents date/times as a 9 element structure) but its likely you really wanted to convert it to POSIXct. as.POSIXct(T$Begin) Also, you might need to use the tz= argument depending on what result you want. See the Help Desk article in RNews 4/1 for more info.
I see that strptime returns a list of
year, mon, mday, hour, min, sec, etc.
The following works for me (for each column that is a date/time field
in my imported file)
cat("Converting date/time fields...\n")
Q = strptime(as.character(data$myfield), format="%Y-%m-%d%H:%M:%S")
data$myfield = ISOdatetime(year = Q$year + 1900,
month = Q$mon + 1, day = Q$mday,
hour =Q$hour, min = Q$min, sec = Q$sec, tz = "")
ISOdatetime does return a vector, which is, I guess, what I want.
It is quite slow like this though, and I don't think it's the best way.
On Thu, 10 Nov 2005, 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)You can avoid all this trouble by using colClasses as documented on the help page.> 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")Or just as.POSIXct(as.character(T$Begin))> 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...It is a list of length 9. Try names(Q)>> 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?You started with POSIXct, and you need to convert back to POSIXct with as.POSIXct(Q). Reading ?DateTimeClasses should explain to you what you are missing. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
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
Possibly Parallel Threads
- time zones in POSIXt
- as.matrix.data.frame() in R 1.9.0 converts to character when it should (?) convert to numeric
- help exporting to excel via xlsReadWritePro
- ISOdate/ISOdatetime performance suggestions, other date/time questions
- Converting a 'difftime' to integer - How to???