Dear R-Experts, seek your help. There are two parts I want to deal with. 1) I want to create a time interval of say, 30 minutes starting from "00:00:00" hrs Thus at the end, I want to create sequence: 00:00:00 00:30:00 01:00:00 01:30:00 .. .. How to do so ? Later, I want to change the time-increment value in a variable and changing the value of this variable, I would like to create new sequence with that time increment. How to use "seq()" correctly? 2) I have a date stored in one variable. Say "2009-01-01" How can I combine this date with each time interval in the first part? Will concatenate work? so at the end, I would like to have: 2009-01-01 00:00:00 2009-01-01 00:30:00 2009-01-01 01:00:00 2009-01-01 01:30:00 ... ... Thank you in advance. -- View this message in context: http://www.nabble.com/How-to-create-sequence-of-constant-time-interval-tp22034441p22034441.html Sent from the R help mailing list archive at Nabble.com.
Gabor Grothendieck
2009-Feb-16 11:04 UTC
[R] How to create sequence of constant time interval
Try this (and see R News 4/1 for more).> library(chron) > tt <- times(0:47/48) > tt[1] 00:00:00 00:30:00 01:00:00 01:30:00 02:00:00 02:30:00 03:00:00 03:30:00 04:00:00 04:30:00 05:00:00 05:30:00 06:00:00 06:30:00 07:00:00 07:30:00 [17] 08:00:00 08:30:00 09:00:00 09:30:00 10:00:00 10:30:00 11:00:00 11:30:00 12:00:00 12:30:00 13:00:00 13:30:00 14:00:00 14:30:00 15:00:00 15:30:00 [33] 16:00:00 16:30:00 17:00:00 17:30:00 18:00:00 18:30:00 19:00:00 19:30:00 20:00:00 20:30:00 21:00:00 21:30:00 22:00:00 22:30:00 23:00:00 23:30:00> chron(rep("1/1/09", length = length(tt)), tt)[1] (01/01/09 00:00:00) (01/01/09 00:30:00) (01/01/09 01:00:00) (01/01/09 01:30:00) (01/01/09 02:00:00) (01/01/09 02:30:00) (01/01/09 03:00:00) [8] (01/01/09 03:30:00) (01/01/09 04:00:00) (01/01/09 04:30:00) (01/01/09 05:00:00) (01/01/09 05:30:00) (01/01/09 06:00:00) (01/01/09 06:30:00) [15] (01/01/09 07:00:00) (01/01/09 07:30:00) (01/01/09 08:00:00) (01/01/09 08:30:00) (01/01/09 09:00:00) (01/01/09 09:30:00) (01/01/09 10:00:00) [22] (01/01/09 10:30:00) (01/01/09 11:00:00) (01/01/09 11:30:00) (01/01/09 12:00:00) (01/01/09 12:30:00) (01/01/09 13:00:00) (01/01/09 13:30:00) [29] (01/01/09 14:00:00) (01/01/09 14:30:00) (01/01/09 15:00:00) (01/01/09 15:30:00) (01/01/09 16:00:00) (01/01/09 16:30:00) (01/01/09 17:00:00) [36] (01/01/09 17:30:00) (01/01/09 18:00:00) (01/01/09 18:30:00) (01/01/09 19:00:00) (01/01/09 19:30:00) (01/01/09 20:00:00) (01/01/09 20:30:00) [43] (01/01/09 21:00:00) (01/01/09 21:30:00) (01/01/09 22:00:00) (01/01/09 22:30:00) (01/01/09 23:00:00) (01/01/09 23:30:00) On Mon, Feb 16, 2009 at 5:00 AM, Suresh_FSFM <suresh.ghalsasi at gmail.com> wrote:> > Dear R-Experts, > > seek your help. > > There are two parts I want to deal with. > 1) > I want to create a time interval of say, 30 minutes starting from "00:00:00" > hrs > Thus at the end, I want to create sequence: > 00:00:00 > 00:30:00 > 01:00:00 > 01:30:00 > .. > .. > How to do so ? > Later, I want to change the time-increment value in a variable and changing > the value of this variable, I would like to create new sequence with that > time increment. How to use "seq()" correctly? > > 2) > I have a date stored in one variable. Say "2009-01-01" > How can I combine this date with each time interval in the first part? Will > concatenate work? > so at the end, I would like to have: > > 2009-01-01 00:00:00 > 2009-01-01 00:30:00 > 2009-01-01 01:00:00 > 2009-01-01 01:30:00 > ... > ... > > Thank you in advance. > > > > > > > -- > View this message in context: http://www.nabble.com/How-to-create-sequence-of-constant-time-interval-tp22034441p22034441.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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. >
Thank you very much for the precise response. Regards, Suresh Suresh_FSFM wrote:> > Dear R-Experts, > > seek your help. > > There are two parts I want to deal with. > 1) > I want to create a time interval of say, 30 minutes starting from > "00:00:00" hrs > Thus at the end, I want to create sequence: > 00:00:00 > 00:30:00 > 01:00:00 > 01:30:00 > .. > .. > How to do so ? > Later, I want to change the time-increment value in a variable and > changing the value of this variable, I would like to create new sequence > with that time increment. How to use "seq()" correctly? > > 2) > I have a date stored in one variable. Say "2009-01-01" > How can I combine this date with each time interval in the first part? > Will concatenate work? > so at the end, I would like to have: > > 2009-01-01 00:00:00 > 2009-01-01 00:30:00 > 2009-01-01 01:00:00 > 2009-01-01 01:30:00 > ... > ... > > Thank you in advance. > > > > > > >-- View this message in context: http://www.nabble.com/How-to-create-sequence-of-constant-time-interval-tp22034441p22035427.html Sent from the R help mailing list archive at Nabble.com.
Something like the following might give a few ideas:> start.date <- '2009-01-01' > start.time <- '00:00:00' > interval <- 30 > > increment.mins <- interval * 60 > x <- paste(start.date, start.time) > > for(i in 1:20) {+ print(strptime(x, "%Y-%m-%d %H:%M:%S") + i*increment.mins) + } [1] "2009-01-01 00:30:00 GMT" [1] "2009-01-01 01:00:00 GMT" [1] "2009-01-01 01:30:00 GMT" [1] "2009-01-01 02:00:00 GMT" [1] "2009-01-01 02:30:00 GMT" [1] "2009-01-01 03:00:00 GMT" [1] "2009-01-01 03:30:00 GMT" [1] "2009-01-01 04:00:00 GMT" [1] "2009-01-01 04:30:00 GMT" [1] "2009-01-01 05:00:00 GMT" [1] "2009-01-01 05:30:00 GMT" [1] "2009-01-01 06:00:00 GMT" [1] "2009-01-01 06:30:00 GMT" [1] "2009-01-01 07:00:00 GMT" [1] "2009-01-01 07:30:00 GMT" [1] "2009-01-01 08:00:00 GMT" [1] "2009-01-01 08:30:00 GMT" [1] "2009-01-01 09:00:00 GMT" [1] "2009-01-01 09:30:00 GMT" [1] "2009-01-01 10:00:00 GMT">On 16 Feb, 10:00, Suresh_FSFM <suresh.ghals... at gmail.com> wrote:> Dear R-Experts, > > seek your help. > > There are two parts I want to deal with. > 1) > I want to create a time interval of say, 30 minutes starting from "00:00:00" > hrs > Thus at the end, I want to create sequence: > 00:00:00 > 00:30:00 > 01:00:00 > 01:30:00 > .. > .. > How to do so ? > Later, I want to change the time-increment value in a variable and changing > the value of this variable, I would like to create new sequence with that > time increment. How to use "seq()" correctly? > > 2) > I have a date stored in one variable. Say "2009-01-01" > How can I combine this date with each time interval in the first part? Will > concatenate work? > so at the end, I would like to have: > > 2009-01-01 00:00:00 > 2009-01-01 00:30:00 > 2009-01-01 01:00:00 > 2009-01-01 01:30:00 > ... > ... > > Thank you in advance. > > -- > View this message in context:http://www.nabble.com/How-to-create-sequence-of-constant-time-interva... > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
ahh, didn't see Gabor's solution, that works much better :-) On 16 Feb, 10:00, Suresh_FSFM <suresh.ghals... at gmail.com> wrote:> Dear R-Experts, > > seek your help. > > There are two parts I want to deal with. > 1) > I want to create a time interval of say, 30 minutes starting from "00:00:00" > hrs > Thus at the end, I want to create sequence: > 00:00:00 > 00:30:00 > 01:00:00 > 01:30:00 > .. > .. > How to do so ? > Later, I want to change the time-increment value in a variable and changing > the value of this variable, I would like to create new sequence with that > time increment. How to use "seq()" correctly? > > 2) > I have a date stored in one variable. Say "2009-01-01" > How can I combine this date with each time interval in the first part? Will > concatenate work? > so at the end, I would like to have: > > 2009-01-01 00:00:00 > 2009-01-01 00:30:00 > 2009-01-01 01:00:00 > 2009-01-01 01:30:00 > ... > ... > > Thank you in advance. > > -- > View this message in context:http://www.nabble.com/How-to-create-sequence-of-constant-time-interva... > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Gabor Grothendieck
2009-Feb-18 18:18 UTC
[R] How to create sequence of constant time interval
For version 2.3-30 of chron which just appeared on CRAN this can
be simplified to:
library(chron)
tt <- times(0:47/48)
tt
chron("1/1/09", tt) # no rep needed
On Mon, Feb 16, 2009 at 6:04 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:> Try this (and see R News 4/1 for more).
>
>> library(chron)
>> tt <- times(0:47/48)
>> tt
> [1] 00:00:00 00:30:00 01:00:00 01:30:00 02:00:00 02:30:00 03:00:00
> 03:30:00 04:00:00 04:30:00 05:00:00 05:30:00 06:00:00 06:30:00
> 07:00:00 07:30:00
> [17] 08:00:00 08:30:00 09:00:00 09:30:00 10:00:00 10:30:00 11:00:00
> 11:30:00 12:00:00 12:30:00 13:00:00 13:30:00 14:00:00 14:30:00
> 15:00:00 15:30:00
> [33] 16:00:00 16:30:00 17:00:00 17:30:00 18:00:00 18:30:00 19:00:00
> 19:30:00 20:00:00 20:30:00 21:00:00 21:30:00 22:00:00 22:30:00
> 23:00:00 23:30:00
>
>> chron(rep("1/1/09", length = length(tt)), tt)
> [1] (01/01/09 00:00:00) (01/01/09 00:30:00) (01/01/09 01:00:00)
> (01/01/09 01:30:00) (01/01/09 02:00:00) (01/01/09 02:30:00) (01/01/09
> 03:00:00)
> [8] (01/01/09 03:30:00) (01/01/09 04:00:00) (01/01/09 04:30:00)
> (01/01/09 05:00:00) (01/01/09 05:30:00) (01/01/09 06:00:00) (01/01/09
> 06:30:00)
> [15] (01/01/09 07:00:00) (01/01/09 07:30:00) (01/01/09 08:00:00)
> (01/01/09 08:30:00) (01/01/09 09:00:00) (01/01/09 09:30:00) (01/01/09
> 10:00:00)
> [22] (01/01/09 10:30:00) (01/01/09 11:00:00) (01/01/09 11:30:00)
> (01/01/09 12:00:00) (01/01/09 12:30:00) (01/01/09 13:00:00) (01/01/09
> 13:30:00)
> [29] (01/01/09 14:00:00) (01/01/09 14:30:00) (01/01/09 15:00:00)
> (01/01/09 15:30:00) (01/01/09 16:00:00) (01/01/09 16:30:00) (01/01/09
> 17:00:00)
> [36] (01/01/09 17:30:00) (01/01/09 18:00:00) (01/01/09 18:30:00)
> (01/01/09 19:00:00) (01/01/09 19:30:00) (01/01/09 20:00:00) (01/01/09
> 20:30:00)
> [43] (01/01/09 21:00:00) (01/01/09 21:30:00) (01/01/09 22:00:00)
> (01/01/09 22:30:00) (01/01/09 23:00:00) (01/01/09 23:30:00)
>
>
> On Mon, Feb 16, 2009 at 5:00 AM, Suresh_FSFM <suresh.ghalsasi at
gmail.com> wrote:
>>
>> Dear R-Experts,
>>
>> seek your help.
>>
>> There are two parts I want to deal with.
>> 1)
>> I want to create a time interval of say, 30 minutes starting from
"00:00:00"
>> hrs
>> Thus at the end, I want to create sequence:
>> 00:00:00
>> 00:30:00
>> 01:00:00
>> 01:30:00
>> ..
>> ..
>> How to do so ?
>> Later, I want to change the time-increment value in a variable and
changing
>> the value of this variable, I would like to create new sequence with
that
>> time increment. How to use "seq()" correctly?
>>
>> 2)
>> I have a date stored in one variable. Say "2009-01-01"
>> How can I combine this date with each time interval in the first part?
Will
>> concatenate work?
>> so at the end, I would like to have:
>>
>> 2009-01-01 00:00:00
>> 2009-01-01 00:30:00
>> 2009-01-01 01:00:00
>> 2009-01-01 01:30:00
>> ...
>> ...
>>
>> Thank you in advance.
>>
>>
>>
>>
>>
>>
>> --
>> View this message in context:
http://www.nabble.com/How-to-create-sequence-of-constant-time-interval-tp22034441p22034441.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> 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.
>>
>
Possibly Parallel Threads
- How to collect arrays in an array?
- help regarding storing time difference values in same unit?
- How to print console output statements from within script or function?
- help regarding converting the available date in "right" date
- missing date and time intervals in data frame