23

I have a vector of times in R, all_symbols$Time and I am trying to find out how to get JUST the times (or convert the times to strings without losing information). I use

strptime(all_symbol$Time[j], format="%H:%M:%S")

which for some reason assumes the date is today and returns

[1] "2013-10-18 09:34:16"

Date and time formatting in R is quite annoying. I am trying to get the time only without adding too many packages (really any--I am on a school computer where I cannot install libraries).

Erroldactyl
  • 383
  • 1
  • 7
  • 17
  • What `class` is `all_symbols$Time`? I suspect you want to use `strftime` instead. – James Oct 18 '13 at 23:41
  • @James I am not sure; how can I check? When i type 'A = strftime(all_symbol$Time[j], format="%H:%M:%S")' in I get 'Error in as.POSIXlt.character(as.character(x), ...) : character string is not in a standard unambiguous format' – Erroldactyl Oct 18 '13 at 23:45
  • 1
    `class(all_symbols$Time)` will give you the class. – James Oct 19 '13 at 09:04

2 Answers2

21

Once you use strptime you will of necessity get a date-time object and the default behavior for no date in the format string is to assume today's date. If you don't like that you will need to prepend a string that is the date of your choice.

@James' suggestion is equivalent to what I was going to suggest:

format(all_symbol$Time[j], format="%H:%M:%S")

The only package I know of that has time classes (i.e time of day with no associated date value) is package:chron. However I find that using format as a way to output character values from POSIXt objects lends itself well to functions that require factor input.

In the decade since this was written there is now a package named “hms” that has some sort of facility for hours, minutes, and seconds.

hms: Pretty Time of Day

Implements an S3 class for storing and formatting time-of-day values, based on the 'difftime' class.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • +1 -- And even **chron**, IIRC, represents times as fractions of days, "underneath it all". – Josh O'Brien Oct 18 '13 at 23:52
  • 1
    @BondedDust, that doesn't work for plotting purposes! – Ehsan M. Kermani Mar 10 '15 at 21:02
  • Your comment has no context! If using `axis` you would need an appropriate `at` argument which was numeric and a `label` argument which was character. If using some other graphics function you would need to match the x-argument to the plot function with the at-argument for that axis function whatever those arguments might be named. – IRTFM Mar 10 '15 at 21:37
  • @Ehsan, for plotting purposes, perhaps putting all your times on the same day might work. Something like `tt <- ymd_hm(str_c('2000-01-01', sample.time, sep = ' '))` (using the lubridate and stringr packages). Then process `tt` to get the plot you need e.g. a histogram of time of day measurements were make. You'll need to fix the axis labelling. – Tony Ladson Feb 22 '17 at 04:42
4

Came across the same problem recently and found this and other posts R: How to handle times without dates? inspiring. I'd like to contribute a little for whoever has similar questions.

If you only want to you base R, take advantage of as.Date(..., format = ("...")) to transform your date into a standard format. Then, you can use substr to extract the time. e.g. substr("2013-10-01 01:23:45 UTC", 12, 16) gives you 01:23.

If you can use package lubridate, functions like mdy_hms will make life much easier. And substr works most of the time.

If you want to compare the time, it should work if they are in Date or POSIXt objects. If you only want the time part, maybe force it into numeric (you may need to transform it back later). e.g. as.numeric(hm("00:01")) gives 60, which means it's 60 seconds after 00:00:00. as.numeric(hm("23:59")) will give 86340.

wxxyyyzz
  • 241
  • 1
  • 7