[R] converting to POSIXct

Stefano Sofia @te|@no@@o||@ @end|ng |rom reg|one@m@rche@|t
Tue Nov 30 12:47:55 CET 2021


Thanks to all of you.
Stefano

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

________________________________________
Da: PIKAL Petr [petr.pikal using precheza.cz]
Inviato: martedì 30 novembre 2021 12.45
A: Duncan Murdoch; Jim Lemon
Cc: r-help mailing list; Stefano Sofia
Oggetto: RE: [R] converting to POSIXct

Hi,

that is what I tried to show to Stefano. That the issue was only with printing the values on console or as you explained in more depth the default formating when all time values (HMS) are zero.

So that Stefano used correct syntax.

Cheers
Petr

> -----Original Message-----
> From: Duncan Murdoch <murdoch.duncan using gmail.com>
> Sent: Tuesday, November 30, 2021 12:05 PM
> To: Jim Lemon <drjimlemon using gmail.com>; PIKAL Petr
> <petr.pikal using precheza.cz>
> Cc: r-help mailing list <r-help using r-project.org>; Stefano Sofia
> <stefano.sofia using regione.marche.it>
> Subject: Re: [R] converting to POSIXct
>
> On 30/11/2021 3:41 a.m., Jim Lemon wrote:
> > Hi,
> > Petr is right. Apparently as.POSIXct drops the smallest increments if
> > all are zero:
>
> That's not as.POSIXct doing anything:  there's no way to drop increments, the
> POSIXct format records a number of seconds and that can't be changed.
>
> What is happening is simply the default formatting.
>
> Be explicit about the format if you want to see the seconds, e.g.
>
>  > format(ssdf$data_POSIX, format = '%Y-%m-%d %H:%M:%S') [1] "2002-11-
> 01 00:00:00" "2002-11-01 00:00:00"
>
> Duncan Murdoch
>
> >
> > ssdf<-read.csv(text="data_POSIX,Sensor_code,value
> > 2002-11-01 00:00:01,1694,7.2
> > 2002-11-01 00:00:00,1723,10.8",
> > stringsAsFactors=FALSE)
> > ssdf$data_POSIX<-as.POSIXct(ssdf$data_POSIX,"%Y-%m-%d HH:MM:SS")
> ssdf
> >
> >             data_POSIX Sensor_code value
> > 1 2002-11-01 00:00:01        1694   7.2
> > 2 2002-11-01 00:00:00        1723  10.8
> >
> > but if there is a single small increment, they all show up.
> >
> > Jim
> >
> > On Tue, Nov 30, 2021 at 7:33 PM PIKAL Petr <petr.pikal using precheza.cz>
> wrote:
> >>
> >> Hi
> >>
> >> You probably has zero hours in all your data
> >>
> >> see
> >>> temp
> >>             data_POSIX Sensor_code value
> >> 1 2002-11-01 00:00:00        1694   7.2
> >> 2 2002-11-01 00:00:00        1723  10.8
> >>
> >> without hours
> >>> as.POSIXct(temp$data_POSIX, format = "%Y-%m-%d %H:%M:%S",
> >>> tz="Etc/GMT-1")
> >> [1] "2002-11-01 +01" "2002-11-01 +01"
> >>
> >> add value to hours
> >>> fix(temp)
> >>> temp
> >>             data_POSIX Sensor_code value
> >> 1 2002-11-01 00:01:00        1694   7.2
> >> 2 2002-11-01 00:00:00        1723  10.8
> >>
> >> Voila, hours are back.
> >>> as.POSIXct(temp$data_POSIX, format = "%Y-%m-%d %H:%M:%S",
> >>> tz="Etc/GMT-1")
> >> [1] "2002-11-01 00:01:00 +01" "2002-11-01 00:00:00 +01"
> >>
> >> So nothing wrong in uyour code, hours are there but they are probably
> not printed to console and hours are there but hidden.
> >>
> >> Cheers
> >> Petr
> >>
> >>> -----Original Message-----
> >>> From: R-help <r-help-bounces using r-project.org> On Behalf Of Stefano
> >>> Sofia
> >>> Sent: Tuesday, November 30, 2021 9:20 AM
> >>> To: r-help mailing list <r-help using r-project.org>
> >>> Subject: [R] converting to POSIXct
> >>>
> >>> Dear R-list users,
> >>> I thought I was able to manage easily POSIXct, but this is not true.
> >>> I am not going to load the input txt file because I know that
> >>> attachments are not allowed. The structure of my input txt file is
> >>>
> >>> data_POSIX,Sensor_code,value
> >>> 2002-11-01 00:00:00,1694,7.2
> >>> 2002-11-01 00:00:00,1723,10.8
> >>> ...
> >>>
> >>> I load it with
> >>> myfile <- read.table(file="mypath/myfile.txt", header = TRUE,
> >>> sep=",", dec = ".", stringsAsFactors = FALSE)
> >>>
> >>> When I try to convert the data_POSIX column (which is a character)
> >>> to POSIXct with
> >>>
> >>> myfile$data_POSIX <- as.POSIXct(myfile$data_POSIX, format =
> >>> "%Y-%m-%d %H:%M:%S", tz="Etc/GMT-1")
> >>>
> >>> the outupt is
> >>>
> >>> 2002-11-01 1694 7.2
> >>> 2002-11-01 1723 10.8
> >>> ...
> >>>
> >>> Why I keep loosing hours, minutes and seconds? Wher eis my mistake
> >>> or my misunderstanding?
> >>>
> >>> Sorry again if I have not been able to reproduce the R code, and
> >>> thank you for your support.
> >>> Stefano
> >>>
> >>>           (oo)
> >>> --oOO--( )--OOo--------------------------------------
> >>> Stefano Sofia PhD
> >>> Civil Protection - Marche Region - Italy Meteo Section Snow Section
> >>> Via del 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. 6 della DGR n. 1394/2008 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.
> >>>
> >>> --
> >>> Questo messaggio  stato analizzato da Libraesva ESG ed  risultato non
> infetto.
> >>> This message was scanned by Libraesva ESG and is believed to be clean.
> >>>
> >>>
> >>>        [[alternative HTML version deleted]]
> >>>
> >>> ______________________________________________
> >>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >>>  https://urlsand.esvalabs.com/?u=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&e=5a635173&h=06ff70f3&f=y&p=y
> >>> PLEASE do read the posting guide  https://urlsand.esvalabs.com/?u=http%3A%2F%2Fwww.R-project.org%2Fposting-&e=5a635173&h=9e88a0d1&f=y&p=y
> >>> guide.html and provide commented, minimal, self-contained,
> >>> reproducible code.
> >> ______________________________________________
> >> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >>  https://urlsand.esvalabs.com/?u=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&e=5a635173&h=06ff70f3&f=y&p=y
> >> PLEASE do read the posting guide
> >>  https://urlsand.esvalabs.com/?u=http%3A%2F%2Fwww.R-project.org%2Fposting-guide.html&e=5a635173&h=e12f63e8&f=y&p=y
> >> and provide commented, minimal, self-contained, reproducible code.
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >  https://urlsand.esvalabs.com/?u=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&e=5a635173&h=06ff70f3&f=y&p=y
> > PLEASE do read the posting guide
> >  https://urlsand.esvalabs.com/?u=http%3A%2F%2Fwww.R-project.org%2Fposting-guide.html&e=5a635173&h=e12f63e8&f=y&p=y
> > and provide commented, minimal, self-contained, reproducible code.
> >


________________________________

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. 6 della DGR n. 1394/2008 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.

--
Questo messaggio  stato analizzato da Libraesva ESG ed  risultato non infetto.
This message was scanned by Libraesva ESG and is believed to be clean.



More information about the R-help mailing list