Thank you for your help, as always.

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: [email protected]
---Oo---------oO----------------------------------------


________________________________
Da: Rui Barradas <[email protected]>
Inviato: venerdì 14 novembre 2025 20:26
A: Stefano Sofia; [email protected]
Oggetto: Re: [R] managing dates in POSIXct format at 00

[You don't often get email from [email protected]. Learn why this is 
important at https://aka.ms/LearnAboutSenderIdentification ]

Às 17:30 de 14/11/2025, Stefano Sofia via R-help escreveu:
> 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: [email protected]
> ---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]]
>
>
> ______________________________________________
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.r-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Hello,

Instead of as.POSIXct use strftime. In the second code line below.



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 <- strftime(mydf$date, format = "%Y-%m-%d %H:%M:%S",
tz="Etc/GMT-1")

max(mydf$data_POSIX)
#> [1] "2007-11-20 00:00:00"

write.table(mydf, file="mydf.txt", sep = ",", dec=".", row.names=FALSE,
col.names = TRUE, quote=FALSE)
mydf2 <- read.table(file="mydf.txt", header = TRUE, sep=",", dec = ".")
mydf2
#>                      date value          data_POSIX
#> 1 2007-11-19 00:00:00.000    10 2007-11-19 00:00:00
#> 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 2007-11-20 00:00:00

mydf2$data_POSIX <- as.POSIXct(mydf2$data_POSIX, format = "%Y-%m-%d
%H:%M:%S", tz="Etc/GMT-1")
mydf2
#>                      date value          data_POSIX
#> 1 2007-11-19 00:00:00.000    10 2007-11-19 00:00:00
#> 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 2007-11-20 00:00:00



Also, since you are writing to and reading from a CSV file, use
write.csv and read.csv.


write.csv(mydf, file="mydf.txt", row.names=FALSE, quote=FALSE)
mydf2 <- read.csv(file="mydf.txt")


Hope this helps,

Rui Barradas





________________________________

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

______________________________________________
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to