-1

I've the data below:

> head(dataraw)
   GMT_DATE GMT_TIME  LATITUDE LONGITUDE
1: 5/5/2016 13:00:30 -2.156247  35.44368
2: 5/5/2016 14:00:24 -2.202152  35.44438
3: 5/6/2016 05:02:48 -2.144972  35.56301
4: 5/6/2016 06:00:30 -2.257708  35.46812
5: 5/6/2016 07:00:24 -2.427338  35.37436
6: 5/6/2016 08:00:59 -2.408157  35.34967

And I'm using the following code to merge columns GMT_DATE and GMT_TIME. Please find a sample of the output as well:

> dataraw$`Date & Time [Local]`<-as.POSIXct(paste(dataraw$GMT_DATE, dataraw$GMT_TIME), format="%m-%d-%y %H:%M:%S")
> head(dataraw)
   GMT_DATE GMT_TIME  LATITUDE LONGITUDE Date & Time [Local]
1: 5/5/2016 13:00:30 -2.156247  35.44368                <NA>
2: 5/5/2016 14:00:24 -2.202152  35.44438                <NA>
3: 5/6/2016 05:02:48 -2.144972  35.56301                <NA>
4: 5/6/2016 06:00:30 -2.257708  35.46812                <NA>
5: 5/6/2016 07:00:24 -2.427338  35.37436                <NA>
6: 5/6/2016 08:00:59 -2.408157  35.34967                <NA>

I used an answer from a different post but the code is not working for my data. Any ideas or what could I be doing wrong?

Any input is appreciated

juansalix
  • 503
  • 1
  • 8
  • 21

1 Answers1

1

This is because of the date separator that is being used (/) and the format of the year (4 digits)

# format should be format="%m/%d/%Y %H:%M:%S

dataraw$`Date & Time [Local]`<-as.POSIXct(paste(dataraw$GMT_DATE, dataraw$GMT_TIME), 
                                      format="%m/%d/%Y %H:%M:%S")
> dataraw
  V1 GMT_DATE GMT_TIME  LATITUDE LONGITUDE Date & Time [Local]
1 1: 5/5/2016 13:00:30 -2.156247  35.44368 2016-05-05 13:00:30
2 2: 5/5/2016 14:00:24 -2.202152  35.44438 2016-05-05 14:00:24
3 3: 5/6/2016 05:02:48 -2.144972  35.56301 2016-05-06 05:02:48
4 4: 5/6/2016 06:00:30 -2.257708  35.46812 2016-05-06 06:00:30
5 5: 5/6/2016 07:00:24 -2.427338  35.37436 2016-05-06 07:00:24
6 6: 5/6/2016 08:00:59 -2.408157  35.34967 2016-05-06 08:00:59
NM_
  • 1,887
  • 3
  • 12
  • 27