Hello! If I would like to generate a sequence of seconds for a date, I would do the following: x <- seq(from=as.POSIXct(2014-08-12 00:00:00),to=as.POSIXct(2014-08-12 23:59:59),by="secs") What if I just want the seconds vector without the date, please? Is there a convenient way to create such a vector, please? thanks, Erin -- Erin Hodgess Associate Professor Department of Mathematical and Statistics University of Houston - Downtown mailto: erinm.hodgess at gmail.com [[alternative HTML version deleted]]
On Aug 12, 2014, at 1:51 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:> Hello! > > If I would like to generate a sequence of seconds for a date, I would do > the following: > > x <- seq(from=as.POSIXct(2014-08-12 00:00:00),to=as.POSIXct(2014-08-12 > 23:59:59),by="secs") > > What if I just want the seconds vector without the date, please? Is there > a convenient way to create such a vector, please? > > thanks, > ErinErin, Do you want just the numeric vector of seconds, with the first value being 0, incrementing by 1 to the final value? x <- seq(from = as.POSIXct("2014-08-12 00:00:00"), to = as.POSIXct("2014-08-12 23:59:59"), by = "secs")> head(x)[1] "2014-08-12 00:00:00 CDT" "2014-08-12 00:00:01 CDT" [3] "2014-08-12 00:00:02 CDT" "2014-08-12 00:00:03 CDT" [5] "2014-08-12 00:00:04 CDT" "2014-08-12 00:00:05 CDT"> tail(x)[1] "2014-08-12 23:59:54 CDT" "2014-08-12 23:59:55 CDT" [3] "2014-08-12 23:59:56 CDT" "2014-08-12 23:59:57 CDT" [5] "2014-08-12 23:59:58 CDT" "2014-08-12 23:59:59 CDT"> head(as.numeric(x - x[1]))[1] 0 1 2 3 4 5> tail(as.numeric(x - x[1]))[1] 86394 86395 86396 86397 86398 86399 Regards, Marc Schwartz
> What if I just want the seconds vector without the date, please? Is there > a convenient way to create such a vector, please?Why do you want such a thing? E.g., do you want it to print the time of day without the date? Or are you trying to avoid numeric problems when you do regressions with the seconds-since-1970 numbers around 1414918800? Or is there another problem you want solved? Note that the number of seconds in a day depends on the day and the time zone. In US/Pacific time I get: > length(seq(from=as.POSIXct("2014-08-12 00:00:00"),to=as.POSIXct("2014-08-12 23:59:59"), by="secs")) [1] 86400 > length(seq(from=as.POSIXct("2014-03-09 00:00:00"),to=as.POSIXct("2014-03-09 23:59:59"), by="secs")) [1] 82800 > length(seq(from=as.POSIXct("2014-11-02 00:00:00"),to=as.POSIXct("2014-11-02 23:59:59"), by="secs")) [1] 90000 Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, Aug 12, 2014 at 11:51 AM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:> Hello! > > If I would like to generate a sequence of seconds for a date, I would do > the following: > > x <- seq(from=as.POSIXct(2014-08-12 00:00:00),to=as.POSIXct(2014-08-12 > 23:59:59),by="secs") > > What if I just want the seconds vector without the date, please? Is there > a convenient way to create such a vector, please? > > thanks, > Erin > > > -- > Erin Hodgess > Associate Professor > Department of Mathematical and Statistics > University of Houston - Downtown > mailto: erinm.hodgess at gmail.com > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
On Tue, Aug 12, 2014 at 1:51 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:> Hello! > > If I would like to generate a sequence of seconds for a date, I would do > the following: > > x <- seq(from=as.POSIXct(2014-08-12 00:00:00),to=as.POSIXct(2014-08-12 > 23:59:59),by="secs") > > What if I just want the seconds vector without the date, please? Is there > a convenient way to create such a vector, please? > > thanks, > Erin > > > -- > Erin HodgessI'm a bit confused by this request. The definition of a POSIXct is: Class "POSIXct" represents the (signed) number of seconds since the beginning of 1970 (in the UTC time zone) as a numeric vector. So I don't really know what you mean by the "seconds portion". There are 24*60*60 or 86,400 seconds in a day. Those seconds are from +0 at 00:00:00 to +86399 for 23:59:59. Is this what you were asking? seconds_vector <-0:86399; #is the simple way to get the above. By the definition given above, there is no such thing as a POSIXct value without a date portion. Any number value will convert to a date+time. Like a timestamp variable in SQL vs. a time variable. If you want to display the seconds_vector as "HH:MM:SS" for some reason, the simple way is: character_time=sprintf("%02d:%02d:%02d", # C-style formatting string seconds_vector/3600, # hour value (seconds_vector%%3600)/60, #minute value seconds_vector%%60); #second value You can simply make that a function getTimePortion <- function(POSIXct_value) { value_in_seconds=as.integer(POSIXct_value); sprintf("%02d:%02d:%02d", # C-style formatting string seconds_vector/3600, # hour value (seconds_vector%%3600)/60, #minute value seconds_vector%%60); #second value }; -- There is nothing more pleasant than traveling and meeting new people! Genghis Khan Maranatha! <>< John McKown