Example - Date


  • Use funtion

 > d1 <- date()  
 > class(d1)  
 [1] "character"  
 > d1  
 [1] "Tue Dec 23 17:46:34 2014"  
 > d2 <- Sys.Date()  
 > class(d2)  
 [1] "Date"  
 > d2  
 [1] "2014-12-23"  
 > format(d2,"%a %b %d")  
 [1] "Tue Dec 23"  
 > d3 <- "1jan2014"  
 > d4 <- as.Date(d3,"%d%b%Y")  
 > d4  
 [1] "2014-01-01"  
 > d2 - d4  
 Time difference of 356 days  
 > as.numeric(d2-d4)  
 [1] 356  
 > weekdays(d2)  
 [1] "Tuesday"  
 > months(d2)  
 [1] "December"  
 > julian(d2)  # days from origin date (1970-01-01)
 [1] 16427  
 attr(,"origin")  
 [1] "1970-01-01"  

  • Use library

 > library(lubridate)  
 > ymd("20140108")  
 [1] "2014-01-08 UTC"  
 > ymd_hms("2014-01-01 10:10:10")  
 [1] "2014-01-01 10:10:10 UTC"  
 > ymd_hms("2014-01-01 10:10:10", tz = "Pacific/Auckland")  
 [1] "2014-01-01 10:10:10 NZDT"  
 > wday( ymd("20140108"))  
 [1] 4  
 > wday( ymd("20140108"), label = TRUE)  
 [1] Wed  
 Levels: Sun < Mon < Tues < Wed < Thurs < Fri < Sat  

No comments:

Post a Comment