Skip to content

Instantly share code, notes, and snippets.

@git-er
Forked from mollietaylor/dateFormats.R
Created April 21, 2021 21:17
Show Gist options
  • Save git-er/9dbf4126aeb7a766ee2cfd1471a0349f to your computer and use it in GitHub Desktop.
Save git-er/9dbf4126aeb7a766ee2cfd1471a0349f to your computer and use it in GitHub Desktop.
Date Formats in R
# 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)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment