[R] Query about use of format in strptime

MacQueen, Don macqueen1 at llnl.gov
Mon Apr 11 18:20:48 CEST 2016


First I added one row to your data, to illustrate a case with missing
times:

year month day hh mm hs
2007 11 19 0 0 0.00
2007 11 19 0 30 0.00
2007 11 19 1 0 0.00
2007 11 19 1 30 0.00
2007 11 19 2 0 0.00
2007 11 19 2 30 0.00
2007 11 19 3 0 0.00
2007 11 19 3 30 0.00
2007 11 19 4 0 0.00
2007 11 19 4 30 0.00
2007 11 19 6 30 0.00

(and I put it in a separate file named snowday.dat)

Then try this:

sd <- read.table('snowday.dat', sep=' ', head=TRUE)
sd$tm <- as.POSIXct( paste(sd$year, sd$month, sd$day, sd$hh, sd$mm,
sep='-'), format='%Y-%m-%d-%H-%M')
dft <- data.frame( tm=seq(min(sd$tm), max(sd$tm), by='30 min') )
sd <- merge(sd, dft, all=TRUE)



This appears to do what you are asking for (if I understand correctly).

> sd
                    tm year month day hh mm hs
1  2007-11-19 00:00:00 2007    11  19  0  0  0
2  2007-11-19 00:30:00 2007    11  19  0 30  0
3  2007-11-19 01:00:00 2007    11  19  1  0  0
4  2007-11-19 01:30:00 2007    11  19  1 30  0
5  2007-11-19 02:00:00 2007    11  19  2  0  0
6  2007-11-19 02:30:00 2007    11  19  2 30  0
7  2007-11-19 03:00:00 2007    11  19  3  0  0
8  2007-11-19 03:30:00 2007    11  19  3 30  0
9  2007-11-19 04:00:00 2007    11  19  4  0  0
10 2007-11-19 04:30:00 2007    11  19  4 30  0
11 2007-11-19 05:00:00   NA    NA  NA NA NA NA
12 2007-11-19 05:30:00   NA    NA  NA NA NA NA
13 2007-11-19 06:00:00   NA    NA  NA NA NA NA
14 2007-11-19 06:30:00 2007    11  19  6 30  0



Notes:
There is no need to use factor()
As David said, don't use POSIXlt. Use POSIXct instead.

-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 4/11/16, 1:48 AM, "R-help on behalf of Stefano Sofia"
<r-help-bounces at r-project.org on behalf of
stefano.sofia at regione.marche.it> wrote:

>Dear Jim and dear Enrico,
>thank you for your replies.
>Unfortunately your hints didn't solve my problem, and I am getting mad.
>Can I show you my whole process? I will be as quick as possible.
>I start from a data frame called Snow of the form
>
>year month day hh mm hs
>2007 11 19 0 0 0.00
>2007 11 19 0 30 0.00
>2007 11 19 1 0 0.00
>2007 11 19 1 30 0.00
>2007 11 19 2 0 0.00
>2007 11 19 2 30 0.00
>2007 11 19 3 0 0.00
>2007 11 19 3 30 0.00
>2007 11 19 4 0 0.00
>2007 11 19 4 30 0.00
>...
>
>whth semi-hourly data.
>I need to deal with date so I used strptime:
>
>Snow$data_factor <- as.factor(paste(Snow$year, Snow$month, Snow$day,
>Snow$hh, Snow$mm, sep="-"))
>Snow$data_strptime <- strptime(Snow$data_factor, format =
>"%Y-%m-%d-%H-%M")
>
>It gives me
>
>year month day hh mm hs  data_factor  data_strptime
>1     2007    11  19  0  0  0  2007-11-19-0-0 2007-11-19 00:00:00
>2     2007    11  19  0 30  0  2007-11-19-0-30  2007-11-19 00:30:00
>3     2007    11  19  1  0  0  2007-11-19-1-0  2007-11-19 01:00:00
>4     2007    11  19  1 30  0  2007-11-19-1-30  2007-11-19 01:30:00
>5     2007    11  19  2  0  0  2007-11-19-2-0  2007-11-19 02:00:00
>6     2007    11  19  2 30  0  2007-11-19-2-30  2007-11-19 02:30:00
>7     2007    11  19  3  0  0  2007-11-19-3-0  2007-11-19 03:00:00
>8     2007    11  19  3 30  0  2007-11-19-3-30  2007-11-19 03:30:00
>9     2007    11  19  4  0  0  2007-11-19-4-0  2007-11-19 04:00:00
>10   2007    11  19  4 30  0  2007-11-19-4-30  2007-11-19 04:30:00
>...
>
>The type of the column data_strptime is
>$data_strptime
>[1] "POSIXlt" "POSIXt"
>
>Because of some days (or part of them) might be missing, given a time
>interval I want to create a new data frame with all time-steps and then
>merge the new data frame with the old one.
>In order to create a new data frame with all time-steps, I thought to use
>
>df_new <- data.frame(data_strptime=seq(init_day, fin_day, by="30 mins"))
>
>and then
>
>Snow_all <- merge(df_new, Snow, by=("data_strptime"), all.x=TRUE)
>
>My problem is in dealing with init_day and fin_day, respectively for
>example "200711190000" and "200711210000".
>I am not able to create a sequence of class "POSIXlt" "POSIXt", in order
>to merge the two data frames.
>
>Could you please help me in this?
>Thank you again for your attention
>Stefano
>
>
>________________________________________
>Da: Jim Lemon [drjimlemon at gmail.com]
>Inviato: lunedì 11 aprile 2016 9.47
>A: Stefano Sofia
>Cc: r-help at r-project.org
>Oggetto: Re: [R] Query about use of format in strptime
>
>Hi Stefano,
>As the help page says:
>
>"The default for the format methods is "%Y-%m-%d %H:%M:%S" if any
>element has a time component which is not midnight, and "%Y-%m-%d"
>otherwise. This is because when the result is printed, it uses the
>default format. If you want a specified output representation:
>
>format(strptime(init_day, format="%Y-%m-%d-%H-%M"),"%Y-%M-%d %H:%M")
>[1] "2015-30-24 00:30"
>
>For the "midnight" case:
>
>format(strptime(init_day, format="%Y-%m-%d-%H-%M"),"%Y-%m-%d %H:%M")
>[1] "2015-02-24 00:00"
>
>Jim
>
>
>On Mon, Apr 11, 2016 at 5:22 PM, Stefano Sofia
><stefano.sofia at regione.marche.it> wrote:
>> Dear R-list users,
>> I need to use strptime because I have to deal with date with hours and
>>minutes.
>> I read the manual for strptime and I also looked at many examples, but
>>when I try to apply it to my code, I always encounter some problems.
>> I try to change the default format, with no success. Why? How can I
>>change the format?
>>
>> 1.
>> init_day <- as.factor("2015-02-24-00-30")
>> strptime(init_day, format="%Y-%m-%d-%H-%M")
>> [1] "2015-02-24 00:30:00"
>> It works, but why also seconds are shown if in format seconds are not
>>specified?
>>
>> 2.
>> init_day <- as.factor("2015-02-24-0-00")
>> strptime(init_day, format="%Y-%m-%d-%H-%M")
>> [1] "2015-02-24"
>> Again, the specified format is not applied. Why?
>>
>> Thank you for your attention and your help
>> Stefano
>>
>>
>> ________________________________
>>
>> 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.
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at 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.
>
>________________________________
>
>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.
>
>______________________________________________
>R-help at 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