2

I have time imported from a file. The column is considered numeric and is as follows:

head(RawData$TimeStart, 10) [1] 0.00 0.00 0.00 30.19 0.00 25.00 0.00 9.20 0.00 0.00

This data represents a sample taken at 0.0 minutes and at 30 minutes, 19 seconds (30.19) for example.

I wish this column to now be converted to the following format:

> head(RawData$Converted, 10)
 [1] "2017-07-05 00:00:00 AEST" "2017-07-05 00:00:00 AEST" "2017-07-05 00:00:00 AEST" "2017-07-05 00:30:19 AEST"
 [5] "2017-07-05 00:00:00 AEST" "2017-07-05 00:25:00 AEST" "2017-07-05 00:00:00 AEST" "2017-07-05 00:09:20 AEST"
 [9] "2017-07-05 00:00:00 AEST" "2017-07-05 00:00:00 AEST"

I have tried the following: RawData$Converted <- as.POSIXct(RawData$TimeStart, origin='1970-01-01', tz='Australia/Darwin')

But this does not give today's date/ time. I am very confused, so any help would be greatly appreciated.

zx8754
  • 52,746
  • 12
  • 114
  • 209
user2716568
  • 1,866
  • 3
  • 23
  • 38
  • Is that hour.minute ? – akrun Jul 05 '17 at 04:24
  • 3
    0.00 = 7 seconds past midnight? I think you are either not showing us enough decimals, or your intended result is wrong. By the way, you are showing `head(TimeStart)` and then `head(TimeEnd)` - is that intentional? – thelatemail Jul 05 '17 at 04:25
  • It is minute.second - the format is from another imported source and not my own! The column AnalysedData$TimeEnd is not my intended output, more I want the same format as that column - as I have stated above. – user2716568 Jul 05 '17 at 04:28
  • Try `.POSIXct(86400*v1, tz = 'Australia/Darwin')` – akrun Jul 05 '17 at 04:29
  • Thanks but unfortunately that still gives the date as 1970-01-01 and not today, as per the format of my other column. – user2716568 Jul 05 '17 at 04:31
  • That you can do with `Sys.Date() +` – akrun Jul 05 '17 at 04:33
  • How would I use Sys.Date() + in conjunction with your answer above? Sorry, I am unclear on this. – user2716568 Jul 05 '17 at 04:35
  • @user2716568 - can you please edit your question a bit to make this clear once and for all. Is your raw data a character string like `"30.19"` or a numeric value `30.19`? Is it measured in `mins.secs` or `mins.fraction_of_mins`. And what is an example of the *exact* output you expect for something like `"30.19"` / `30.19`? `"2017-07-05 00:30:19 ACST"` or `"2017-07-05 00:30:11 ACST"` or something else? – thelatemail Jul 05 '17 at 04:42
  • Thank you for the feedback, I have edited the question to provide more information. – user2716568 Jul 05 '17 at 04:51

1 Answers1

3

Break your numeric values to convert them all to seconds, and add that to a datetime for today:

x <- c(0.00,0.00,0.00,30.19,0.00,25.00,0.00,9.20,0.00,0.00)
as.POSIXct(format(Sys.Date()), tz="Australia/Darwin") + (floor(x)*60) + ((x-floor(x))*100)
# [1] "2017-07-05 00:00:00 ACST" "2017-07-05 00:00:00 ACST" "2017-07-05 00:00:00 ACST" "2017-07-05 00:30:19 ACST"
# [5] "2017-07-05 00:00:00 ACST" "2017-07-05 00:25:00 ACST" "2017-07-05 00:00:00 ACST" "2017-07-05 00:09:20 ACST"
# [9] "2017-07-05 00:00:00 ACST" "2017-07-05 00:00:00 ACST"

This is in Darwin time, ACST instead of AEST, but I gather that's what you want.

thelatemail
  • 91,185
  • 12
  • 128
  • 188
  • Thank you so very much! If I wanted AEST instead of ACST? – user2716568 Jul 05 '17 at 05:00
  • 2
    see `OlsonNames()` for ALL the possible timezones – SymbolixAU Jul 05 '17 at 05:00
  • 1
    @user2716568 - just specify your timezone explicitly - `tz="Australia/Brisbane"` or `tz="Australia/Sydney"` - just make sure you are not dealing with daylight savings at the time. I usually work in `tz="UTC"` if I do not need to take into account daylight savings. – thelatemail Jul 05 '17 at 05:02