Reputation: 1
My dataframe has this structure:
str(marc)
$ Data : Date, format: "2015-10-31" "2015-10-31" "2015-10-31" ...
$ Hora :Class 'times' atomic [1:351] 0.792 0.792 0.792 0.792 0.5 ...
.. ..- attr(*, "format")= chr "h:m:s"
I am trying to create a new column joining Data and Hora:
marc$Timestamp=as.POSIXct(paste(marc$Data, marc$Hora), format = "%Y-%m-%d %H:%M:%S")
But as.POSIXct is returning NAs.
$ Timestamp: POSIXct, format: NA NA NA ...
I used the same process to create a Timestamp with other dataframe and it have worked. What I am doing wrong this time? Thank you very much!
> dput(marc$Hora)
structure(c(0.791666666666667, 0.791666666666667, 0.791666666666667,
0.791666666666667, 0.5, 0.833333333333333, 0.833333333333333,
0.833333333333333, 0.708333333333333, 0.833333333333333, 0.708333333333333,
0.708333333333333, 0.604166666666667, 0.604166666666667, 0.604166666666667,
0.708333333333333, 0.8125, 0.75, 0.541666666666667, 0.75, 0.541666666666667,
0.541666666666667, 0.541666666666667, 0.8125, 0.8125, 0.520833333333333,
0.8125, 0.8875, 0.9375, 0.9375, 0.9375, 0.8875, 0.895833333333333,
...
format = "h:m:s", class = "times")
Before use POSIXct, I ran:
marc$Hora=times(marc$Hora)
Hora should be H:M:S, but it didn't change
Upvotes: 0
Views: 213
Reputation: 263362
I thought I recognized that class and format as coming from package chron
(not "Chron"):
library(chron)
Hora
# [1] 19:00:00 19:00:00 19:00:00 19:00:00 12:00:00 20:00:00 20:00:00 20:00:00 17:00:00
#[10] 20:00:00 17:00:00 17:00:00 14:30:00 14:30:00 14:30:00 17:00:00 19:30:00 18:00:00
#[19] 13:00:00 18:00:00 13:00:00 13:00:00 13:00:00 19:30:00 19:30:00 12:30:00 19:30:00
#[28] 21:18:00 22:30:00 22:30:00 22:30:00 21:18:00 21:30:00
So they are values that are at the hour boundaries. If you build an example using the str
information with that`knowledge:
marc <- data.frame( Data =as.Date("2015-10-31", "2015-10-31", "2015-10-31"),
Hora=structure(c( 0.792, 0.792, 0.792),format = "h:m:s", class = "times"))
marc
#----------------
Data Hora
1 2018-11-03 19:00:29
2 2018-11-03 19:00:29
3 2018-11-03 19:00:29
The seconds are off a bit by virtue of rounding. At any rate you should coerce the Hora
values to character before paste
-ing:
as.POSIXct( paste(marc$Data, as.character(marc$Hora) ) )
#[1] "2018-11-03 19:00:29 PDT" "2018-11-03 19:00:29 PDT" "2018-11-03 19:00:29 PDT"
Upvotes: 0