user2716568
user2716568

Reputation: 1946

Convert numeric time format to POSIXlt

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.

Upvotes: 2

Views: 124

Answers (1)

thelatemail
thelatemail

Reputation: 93813

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.

Upvotes: 3

Related Questions