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.

Chapter 6Going on a Date with RIn This ChapterWorking with dates in RUnderstanding the different ways of representing datesThrowing time into the mixFormatting dates and times for pretty printingOperating on dates and timesAll kinds of real-world data are associated with a specific date or instant intime. Companies report results each quarter. Stock markets report closing pricesdaily. Network analysts measure traffic by the hour (if not by the minute). And ofcourse, scientists measure air temperature, sometimes by the minute, sometimesby the day, and have done so for decades.Dealing with dates accurately can be a complicated task. You have to accountfor time-zone differences, leap years, and regional differences in holidays. Inaddition, people report data differently in different places. For example, what anAmerican would write as “May 12, 2010” or “05-12-10” would be written bysomeone from the United Kingdom as “12 May 2010” or “12-05-10.” Working with atime instant on a specific day isn’t any easier. The same time may be written as9:25 p.m., 21:25, or 21h25 — not to mention time zones!In this chapter, we look at the different ways of representing dates and timesusing R. You take control of the format of dates and time for pretty printing. Thenyou do some math with dates — addition and subtraction. Finally, you use sometricks to extract specific elements, such as the month, from a date.Working with DatesR has a range of functions that allow you to work with dates and times. Theeasiest way of creating a date is to use the as.Date() function. For example, youwrite the opening day of the 2012 London Olympic Games as:> xd xd

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

Saved successfully!

Ooh no, something went wrong!