dsaiztc
8/18/2015 - 10:31 AM

Dates in R.

Dates in R.

# http://www.r-bloggers.com/date-formats-in-r/

# Importing Dates from Character Format
dates <- c("05/27/84", "07/07/05")
betterDates <- as.Date(dates, format = "%m/%d/%y")

dates <- c("May 27 1984", "July 7 2005")
betterDates <- as.Date(dates, format = "%B %d %Y")

 > betterDates
   [1] "1984-05-27" "2005-07-07"
   
# Importing Dates from Numeric Format
#   from Windows Excel:
dates <- c(30829, 38540)
betterDates <- as.Date(dates, origin = "1899-12-30")

#   from Mac Excel:
dates <- c(29367, 37078)
betterDates <- as.Date(dates, origin = "1904-01-01")

# Changing Date Formats (to use dates in a format other than the standard %Y-%m-%d)
format(betterDates, "%a %b %d")
 [1] "Sun May 27" "Thu Jul 07"