user3791321
user3791321

Reputation:

Convert timestamp to milliseconds

Hey I'm working on SparkR and I have this dataset with data field as this

2013-11-01 00:00

Result from str

Time    : chr  "2013-11-01 00:00" "2013-11-01 00:10" "2013-11-01 00:20" "2013-11-01 00:30" ...

Now I need to convert this data to milliseconds. Note: I need the Hour and minute.

When I tried a simple as.numeric it gives me NA. How can I do?

Ps:I searched similar questions and when I tried some solutions hour and minute disappear also when I user format attribute.

I Did this now I need the inverse procedure from "2013-11-01 09:10" to ->1383293400000

a<-TrentoTo$TimeInterval[1]  #1383293400000
b<-as.POSIXct((a)/1000, origin = "1970-01-01")
b<-strftime(b, format="%Y-%m-%d %H:%M") #"2013-11-01 09:10"

Upvotes: 2

Views: 2801

Answers (1)

Tim
Tim

Reputation: 7474

First you have to convert to POSIXct as in this thread: Dealing with timestamps in R. So the code should be:

as.numeric(as.POSIXct(Time))

Solution using your own example

a <- 1383293400000
b <- as.POSIXct((a)/1000, origin = "1970-01-01")
b <- strftime(b, format="%Y-%m-%d %H:%M") #"2013-11-01 09:10"

outputs

> as.numeric(as.POSIXct(b))*1000 == a
[1] TRUE

Upvotes: 6

Related Questions