O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

Internally, R represents dates as the number of days from January 1, 1970:

  1. Get today's date:
> Sys.Date() 
  1. Create a date object from a string:
> # Supply year as two digits 
> # Note correspondence between separators in the date string and the format string 
> as.Date("1/1/80", format = "%m/%d/%y")  
[1] "1980-01-01" 
 
> # Supply year as 4 digits 
> # Note uppercase Y below instead of lowercase y as above 
> as.Date("1/1/1980", format = "%m/%d/%Y") 
[1] "1980-01-01" 
 
> # If you omit format string, you must give date as "yyyy/mm/dd" or as "yyyy-mm-dd" 
> as.Date("1970/1/1") 
[1] "1970-01-01" 
 
> as.Date("70/1/1") 
[1] "0070-01-01" 
  1. Use other options for separators (this example uses hyphens) in the format string, and also see the underlying ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required