2

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"
zero323
  • 322,348
  • 103
  • 959
  • 935

1 Answers1

5

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
Community
  • 1
  • 1
Tim
  • 7,075
  • 6
  • 29
  • 58
  • Yes I needed this and it works. How can I add more digits? I got this from your code: 1383260400 Need: 1383293400000 –  Nov 30 '15 at 14:30
  • @DanieleS check my edit, if it is not working it means that you have some bug in your code in other place and your example does not reproduce the bug. – Tim Nov 30 '15 at 14:45
  • I badly explained myself. I need to join them later does this notation will create some trouble? 1.383293e+12 join 1383293400000 –  Nov 30 '15 at 14:45
  • @DanieleS scientific notation is just a way of displaying numbers, it does not affect anything anyhow... – Tim Nov 30 '15 at 14:46
  • Thanks for your time. –  Nov 30 '15 at 14:50