R - Date & Time

  • Date
    • Represented by the Date class, stored internally as number of days since 1970-01-01
    •  > x <- as.Date("2014-09-09")  # create date
       > x  
       [1] "2014-09-09"  
       > unclass(x)  
       [1] 16322  # number of days since 1970-01-01
      
  • Time
    • Representd by the POSIXct or POSIXlt class
    • POSIXct: large integer
    •  > y <- Sys.time()  
       > y <- as.POSIXct(y)  
       > names(unclass(y))  
       NULL  
       > unclass(y)  
       [1] 1411258804  
      
    • POSIXlt: with many useful information
    •  > y <- as.POSIXlt(y)  
       > names(unclass(y))  
        [1] "sec"  "min"  "hour"  "mday"  "mon"  "year"   
        [7] "wday"  "yday"  "isdst" "zone"  "gmtoff"  
      
    • Create from string
    •  > time <- c("September 21, 2014 1:27")  
       > x <- strptime(time, "%B %d, %Y %H:%M")  
       > x  
       [1] "2014-09-21 01:27:00 BST"  
       > class(x)  
       [1] "POSIXlt" "POSIXt"   
      
    • Check time differences
    •  > y <- Sys.time()  
       > y  
       [1] "2014-09-21 01:34:42 BST"  
       > x - y  
       Time difference of -7.709296 mins  
      

No comments:

Post a Comment