12.07.2015 Views

R dummies

R dummies

R dummies

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

So, to add seven days to the Apollo moon landing date, use addition, justremember to multiply the number of days by the number of seconds per day:> xct + 7*86400[1] “1969-07-27 20:17:39 UTC”After you know that you can convert any duration to seconds, you can add orsubtract any value to a datetime object. For example, add three hours to the timeof the Apollo moon landing:> xct + 3*60*60[1] “1969-07-20 23:17:39 UTC”Similarly, to get a date seven days earlier, use subtraction:> xct - 7*86400[1] “1969-07-13 20:17:39 UTC”There is an important difference between Date objects and POSIXct orPOSIXlt objects. If you use a Date object, you add and subtract days; withPOSIXct and POSIXlt, the operations add or subtract only seconds.Try that yourself, first converting xct to a Date object, then subtracting 7:> as.Date(xct) - 7[1] “1969-07-13”Comparison of datesSimilar to the way that you can add or subtract states you can also comparedates with the comparison operators, such as less than (),covered in Chapter 5.Say you want to compare the current time with any fixed time. In R, you usethe Sys.time() function to get the current system time:> Sys.time()[1] “2012-03-24 10:12:52 GMT”

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!