Converting time format to numeric with R
Asked Answered
Y

4

11

In most cases, we convert numeric time to POSIXct format using R. However, if we want to compare two time points, then we would prefer the numeric time format. For example, I have a date format like "2001-03-13 10:31:00",

  begin <- "2001-03-13 10:31:00"

Using R, I want to covert this into a numeric (e.g., the Julian time), perhaps something like the passing seconds between 1970-01-01 00:00:00 and 2001-03-13 10:31:00.

Do you have any suggestions?


The Julian calendar began in 45 BC (709 AUC) as a reform of the Roman calendar by Julius Caesar. It was chosen after consultation with the astronomer Sosigenes of Alexandria and was probably designed to approximate the tropical year (known at least since Hipparchus). see http://en.wikipedia.org/wiki/Julian_calendar

Ylangylang answered 16/1, 2012 at 17:6 Comment(4)
I didn't downvote but I suspect it was done because of careless mismatch between your input and output. It seems possible you hoped to just remove the punctuation in a character type vector and you wanted: 20010313103100 possibly as a numeric value but at the moemnt it's very unclear.Burbot
Your reply is helpful. I will revise the question.Ylangylang
Generally speaking, as.numeric is a good first place to turn if you want to convert something to, well, a number. Have you tried that?Phyllode
good reminding. thank u.Ylangylang
B
15

If you just want to remove ":" , " ", and "-" from a character vector then this will suffice:

end <- gsub("[: -]", "" , begin, perl=TRUE)
#> end
#[1] "20010313103100"

You should read the section about 1/4 of the way down in ?regex about character classes. Since the "-" is special in that context as a range operator, it needs to be placed first or last.

After your edit then the answer is clearly what @joran wrote, except that you would need first to convert to a DateTime class:

 as.numeric(as.POSIXct(begin))
#[1] 984497460

The other point to make is that comparison operators do work for Date and DateTime classed variables, so the conversion may not be necessary at all. This compares 'begin' to a time one second later and correctly reports that begin is earlier:

as.POSIXct(begin) < as.POSIXct(begin) +1
 #[1] TRUE
Burbot answered 16/1, 2012 at 17:41 Comment(1)
or more generally gsub("[^[:digit:]]", "", begin) will remove anything other than number.Boni
R
8

Based on the revised question this should do what you want:

begin <- "2001-03-13 10:31:00"
as.numeric(as.POSIXct(begin))

The result is a unix timestamp, the number of seconds since epoch, assuming the timestamp is in the local time zone.

Retrogradation answered 19/7, 2016 at 13:21 Comment(0)
B
3

Maybe this could also work:

library(lubridate)
...
df <- '24:00:00'

as.numeric(hms(df))

hms() will convert your data from one time format into another, this will let you convert it into seconds. See full documentation.

I tried this because i had trouble with data which was in that format but over 24 hours.

Bucksaw answered 24/9, 2019 at 4:46 Comment(0)
C
2

The example from ?as.POSIX help gives

as.POSIXct(strptime(begin, "%Y-%m-%d %H:%M:%S"))

so for you it would be

as.numeric(as.POSIXct(strptime(begin, "%Y-%m-%d %H:%M:%S")))
Crazy answered 18/7, 2017 at 6:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.