[R] converting to POSIXct
Duncan Murdoch
murdoch@dunc@n @end|ng |rom gm@||@com
Tue Nov 30 12:04:58 CET 2021
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://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-
>>> guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list