public
Created

Date Formats in R

  • Download Gist
dateFormats.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
# importing dates:
dates <- c("05/27/84", "07/07/05")
betterDates <- as.Date(dates,
format = "%m/%d/%y") # here you put the format your dates are currently in
# it will output the ISO standard dates (%Y-%m-%d)
 
# or:
 
dates <- c("May 27 1984", "July 7 2005")
betterDates <- as.Date(dates,
format = "%B %d %Y")
betterDates
 
# from Windows Excel:
dates <- c(30829, 38540)
betterDates <- as.Date(dates,
origin = "1899-12-30")
betterDates
 
# from Mac Excel:
dates <- c(29367, 37078)
betterDates <- as.Date(dates,
origin = "1904-01-01")
betterDates
 
# using dates:
format(betterDates,
"%a %b %d")
 
mean(betterDates)
max(betterDates)
min(betterDates)
 
 
# fixing centuries:
dates <- c("05/27/84", "07/07/05", "08/17/20")
betterDates <- as.Date(dates, "%m/%d/%y")
 
correctCentury <- as.Date(ifelse(betterDates > Sys.Date(),
format(betterDates, "19%y-%m-%d"),
format(betterDates)))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.