[R] managing dates in POSIXct format at 00

Stefano Sofia @te|@no@@o||@ @end|ng |rom reg|one@m@rche@|t
Fri Nov 14 18:30:27 CET 2025


Dear R-list users,

this question will seem silly to most of you, but I really got mad trying to find a solution with no success, and therefore I decided to write here.


Suppose I create the following data frame:


mydf <- data.frame(date = c("2007-11-19 00:00:00.000", "2007-11-19 06:00:00.000", "2007-11-19 12:00:00.000", "2007-11-19 18:00:00.000", "2007-11-20 00:00:00.000"), value = rep(10, 5))

mydf$data_POSIX <- as.POSIXct(mydf$date, format = "%Y-%m-%d %H:%M:%S", tz="Etc/GMT-1")

max(mydf$data_POSIX)


gives, obviously,


[1] "2007-11-20 +01"


Now if I export this data frame in a txt file


write.table(mydf, file="mydf.txt", sep = ",", dec=".", row.names=FALSE, col.names = TRUE, quote=FALSE)


the file is stored like


date,value,data_POSIX
2007-11-19 00:00:00.000,10,2007-11-19
2007-11-19 06:00:00.000,10,2007-11-19 06:00:00
2007-11-19 12:00:00.000,10,2007-11-19 12:00:00
2007-11-19 18:00:00.000,10,2007-11-19 18:00:00
2007-11-20 00:00:00.000,10,2007-11-20

Hours, minutes and seconds at 00 are not shown anymore.
When I read back this file into R:

mydf2 <- read.table(file="mydf.txt", header = TRUE, sep=",", dec = ".")

mydf2$data_POSIX <- as.POSIXct(mydf2$data_POSIX, format = "%Y-%m-%d %H:%M:%S", tz="Etc/GMT-1")

I get
                     date value          data_POSIX
1 2007-11-19 00:00:00.000    10                <NA>
2 2007-11-19 06:00:00.000    10 2007-11-19 06:00:00
3 2007-11-19 12:00:00.000    10 2007-11-19 12:00:00
4 2007-11-19 18:00:00.000    10 2007-11-19 18:00:00
5 2007-11-20 00:00:00.000    10                <NA>

and

max(mydf2$data_POSIX)


does not work anymore.

Where is my mistake? Is there a solution?

Thank you for your attention and your help

Stefano





         (oo)
--oOO--( )--OOo--------------------------------------
Stefano Sofia MSc, PhD
Civil Protection Department - Marche Region - Italy
Meteo Section
Snow Section
Via Colle Ameno 5
60126 Torrette di Ancona, Ancona (AN)
Uff: +39 071 806 7743
E-mail: stefano.sofia using regione.marche.it
---Oo---------oO----------------------------------------

________________________________

AVVISO IMPORTANTE: Questo messaggio di posta elettronica pu� contenere informazioni confidenziali, pertanto � destinato solo a persone autorizzate alla ricezione. I messaggi di posta elettronica per i client di Regione Marche possono contenere informazioni confidenziali e con privilegi legali. Se non si � il destinatario specificato, non leggere, copiare, inoltrare o archiviare questo messaggio. Se si � ricevuto questo messaggio per errore, inoltrarlo al mittente ed eliminarlo completamente dal sistema del proprio computer. Ai sensi dell'art. Ai sensi dell'art. 2.4 dell'allegato 1 alla DGR n. 74/2021, si segnala che, in caso di necessit� ed urgenza, la risposta al presente messaggio di posta elettronica pu� essere visionata da persone estranee al destinatario.
IMPORTANT NOTICE: This e-mail message is intended to be received only by persons entitled to receive the confidential information it may contain. E-mail messages to clients of Regione Marche may contain information that is confidential and legally privileged. Please do not read, copy, forward, or store this message unless you are an intended recipient of it. If you have received this message in error, please forward it to the sender and delete it completely from your computer system.

	[[alternative HTML version deleted]]



More information about the R-help mailing list