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]]
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.
On Mon, 11 Apr 2016, Stefano Sofia <stefano.sofia at regione.marche.it> writes:> 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 > Stefanostrptime creates a POSIXlt object, and the specified format tells it how to interpret the string you pass in. (Your factor is converted to character by strptime.) If you want to have the POSIXlt object printed in a particular way, use ?strftime or ?format. > format(strptime(init_day, format="%Y-%m-%d-%H-%M"), "%Y-%m-%d-%H-%M") ## [1] "2015-02-24-00-00" -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net
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.
Seemingly Similar Threads
- Query about use of format in strptime
- Query about use of format in strptime
- Sum of columns of a data frame equal to NA when all the elements are NA
- Create a vector without using an external 'if statement'
- Sum of columns of a data frame equal to NA when all the elements are NA