-1

I have a time in excel that when converter to R, comes as a character and looks someting like this 0.59658.

I am trying to convert to POSIXct but it returns as a POSIXct with NA.

teste <- as.POSIXct(test, format = "%H:%M")

I've also tried teste <- as.POSIXct(test, format = "%H:%M:%S") For other columns it works fine, but not this one..

UPDATE: I've done the solution, but a second problem comes with the rest of the thing that I need.

teste <- as.POSIXct(teste*24*60*60,"%H%M", origin="1970-01-01") 
teste <- format(as.POSIXct(teste, format = "%Y-%m-%d %H:%M:%S"), format="%H:%M")

And now, I want to paste with a date vector that is a POSIXct in the 2013-01-06, with this command:

teste<-as.POSIXct(paste(date, teste), format="%Y-%m-%d %H:%M:%S")

And the NA are back

L8call
  • 87
  • 1
  • 1
  • 6
  • You can perform it in one step using the decimal number for the time and use the date column for the origin: `as.POSIXct(teste*24*60*60,"%H%M", origin=date)` – Dave2e Jun 08 '18 at 18:35

1 Answers1

0

Confused as to what exactly you want, but what is wrong with this function:

df <- data.frame(number = c(0.59658, 0.59658, 0.59658, 0.59658, 0.59658), dates = c("2013-01-06", "2013-01-06", "2013-01-06", "2013-01-07", "2013-01-07"))
testing <- function(number, dates){
  teste <- as.POSIXct(number*24*60*60,"%H%M", origin="1970-01-01") 
  teste <- format(as.POSIXct(teste, format = "%Y-%m-%d %H:%M:%OS"), format="%H:%M")
  return(as.POSIXct(paste0(dates," ",teste)))
}

Which gives the following when doing testing(df$number, df$dates):

 "2013-01-06 14:19:00 EST" "2013-01-06 14:19:00 EST" "2013-01-06 14:19:00 EST" "2013-01-07 14:19:00 EST" "2013-01-07 14:19:00 EST"
Adam Warner
  • 1,334
  • 2
  • 14
  • 30