2012-08-12 8 views
22

Me gustaría agregar 1 hora a un objeto POSIXct, pero no admite '+'.Agregar tiempo al objeto POSIXct en R

Este comando:

as.POSIXct("2012/06/30","GMT") 
    + as.POSIXct(paste(event_hour, event_minute,0,":"), ,"%H:%M:$S") 

devuelve este error:

Error in `+.POSIXt`(as.POSIXct("2012/06/30", "GMT"), as.POSIXct(paste(event_hour, : 
    binary '+' is not defined for "POSIXt" objects 

¿Cómo puedo añadir unas pocas horas a un objeto POSIXct?

Respuesta

46

POSIXct objetos son una medida de segundos de un origen, generalmente la época de UNIX (1 de enero de 1970). Sólo tiene que añadir el número requerido de segundos al objeto:

x <- Sys.time() 
x 
[1] "2012-08-12 13:33:13 BST" 
x + 3*60*60 # add 3 hours 
[1] "2012-08-12 16:33:13 BST" 
+0

acabo entendido esto al mismo tiempo que yo lee tu pregunta: D, ¡gracias por responder! – BlueTrin

33

El paquete lubridate también implementa esta muy bien con funciones de confort, hoursminutes, etc.

x = Sys.time() 
library(lubridate) 
x + hours(3) # add 3 hours 
Cuestiones relacionadas