date format in R [duplicate] - r

This question already has answers here:
Add correct century to dates with year provided as "Year without century", %y
(3 answers)
Closed 5 years ago.
I have a date column in a data frame in chr format as follows:
chr [1:1944] "20-Sep-90" "24-Feb-05" "16-Aug-65" "19-Nov-56" "28-Nov-59" "19-Apr-86"
I want to convert to date using something like:
strptime(x=data$dob, '%d-%b-%y')
But I get several future dates in the result like
[1] "1990-09-20" "2005-02-24" "2065-08-16" "2056-11-19" "2059-11-28" "1986-04-19" "2041-04-01" "1971-01-23"
[9] "1995-11-25" "1995-11-25" "2009-02-11" "2002-09-19" "1977-10-06" "1998-03-22" "2050-03-12" "2030-03-26"
Is there a way to ensure I return dates that commenced in the correct century?
Thanks

It doesn't look (from the documentation for %y in ?strptime) like there's any obvious option for changing the default century inferred from 2-digit years.
Since the objects returned by strptime() have class POSIXlt, though, it's a pretty simple matter to subtract 100 years from any dates after today (or after any other cutoff date you'd like to use).
# Use strptime() to create object of class POSIXlt
dd <- c("20-Sep-90", "24-Feb-05", "16-Aug-65",
"19-Nov-56", "28-Nov-59", "19-Apr-86")
DD <- strptime(dd, '%d-%b-%y')
# Subtract 100 years from any date after today
DD$year <- ifelse(DD > Sys.time(), DD$year-100, DD$year)
DD
[1] "1990-09-20" "2005-02-24" "1965-08-16" "1956-11-19" "1959-11-28" "1986-04-19"

dd <- c("20-Sep-90", "24-Feb-05", "16-Aug-65",
"19-Nov-56", "28-Nov-59", "19-Apr-86")
library(lubridate)
DD=dmy(dd)
https://cran.r-project.org/web/packages/lubridate/vignettes/lubridate.html http://vita.had.co.nz/papers/lubridate.pdf

strptime(data$dob, "%Y/%m/%d")

Related

Convert MM-DD-YYYY into MonthName-DD-YYYY [duplicate]

This question already has answers here:
Changing date format in R
(7 answers)
Closed 1 year ago.
How to convert 10-03-2021 into October-03-2021?
I have column in data frame in MM-DD-YYYY which needs to be converted into Month Name- Date- Year
example
10-03-2021 into October-03-2021
You have two parts: parsing the date, and then printing it with your format.
Parsing is quite easy with the lubridate package:
> library(lubridate)
> dates <- mdy(c('10-03-2021', '01-31-1995'))
> dates
[1] "2021-10-03" "1995-01-31"
For printing, you can then use the format() function:
> format(dates, '%B-%d-%Y')
[1] "October-03-2021" "January-31-1995"
%B is the full name of the month, %d the day and %Y the year (with all 4 digits)

Convert month-year to date format in r [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 1 year ago.
I have a date that is in the this format:
chr [1:56] "Sep-2016" "Oct-2016" "Nov-2016" "Dec-2016" "Jan-2017" "Feb-2017" "Mar-2017" "Apr-2017"
I tried as.Date(Dates, "%b-%Y") and got NA for all the values. For some reason I have tried multiple ways and using different format but it is still not working. I am looking to get it into either 09-2016, 10-2016 or just simply turn it into a date format.
Any help is much appreciated!
Date class needs a day as well. Easiest is to convert to yearmon class from zoo and then coerce it to Date, which adds a dummy day
library(zoo)
as.Date(as.yearmon(Dates, '%b-%Y'))
or in base R, paste a day and convert
as.Date(paste0(Dates, '-01'), '%b-%Y-%d')

Converting YY/MM/DD to MM/DD/YY in R [duplicate]

This question already has answers here:
How to convert date to format "yyyy-mm-dd" in R when input value can be of different formats
(3 answers)
Change Date print format from yyyy-mm-dd to dd-mm-yyyy
(2 answers)
Closed 2 years ago.
I need to find the difference in days between 2 date columns, but one of them is in the format of "6/16/2019" and the other is in the format of "2019-02-25". Not sure which one would be easier to convert to which, but would like to get end result in days. Which I know how to do. I would appreciate help converting the second yyyy-mm-dd to mm-dd-yyyy.
We can use functions from the lubridate package to convert the different formats to dates, and then subtract.
rawData <- "date1,date2
2002-05-15,6/16/2019
2019-12-31,4/15/2020"
data <- read.csv(text = rawData,stringsAsFactors = FALSE)
library(lubridate)
mdy(data$date2) - ymd(data$date1)
...and the output:
> mdy(data$date2) - ymd(data$date1)
Time differences in days
[1] 6241 106
>

Lubridate Date parsing is one year off [duplicate]

This question already has answers here:
date functions in R return wrong year
(2 answers)
Closed 3 years ago.
Within R, I'm trying to convert a text string into a Date variable type using lubridate's as.Date function.
I have a vector of values such as:
Dates
11/28/2019
11/29/2019
I am attempting to convert these to standard date variables using this as.Date function:
as.Date(Dates, "%m/%d/%y")
I do not receive an error message, and it correctly interprets the month and date, but for some reason it's outputting the wrong year - one year ahead:
"2020-11-28"
"2020-11-29"
I have no earthly idea why it is incorrectly interpreting the year in this way. Any help is appreciated!
We need to use %Y for 4 digit year as %y refers to only 2 digit
as.Date(Dates, "%m/%d/%Y")
Or using lubridate, this would be resolved
library(lubridate)
mdy(Dates)
Or with anydate from anytime
library(anytime)
anydate(Dates)

%b-%Y date conversion gives NA [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 4 years ago.
I am trying to convert character strings to Dates in R. These are examples of the character strings:
"Aug-1973" "Aug-1974" "Aug-1975" "Aug-1976" "Aug-1977"
I run the following line on date strings similar to the ones above:
exportsDF$Date <- as.Date(as.character(exportsDF$Date), format = "%b-%Y")
This returns NAs for all values. The step where I convert the dates column to characters returns the correct values. Any ideas why the as.Date() command is not working? There are no NAs or missing values in the data. Every value has a "%b-%Y" format.
Any help is appreciated!
The date format needs a day as well, so you could add an arbitrary day of the month. Here, I've chosen the first day:
dates <- c("Aug-1973", "Aug-1974", "Aug-1975", "Aug-1976", "Aug-1977")
res <- as.Date(paste0("01-", dates), format = "%d-%b-%Y")
print(res)
#[1] "1973-08-01" "1974-08-01" "1975-08-01" "1976-08-01" "1977-08-01"
The reason is that the underlying Date data type is an integer counting the days since some reference day. Specifically, the number of days since 1970-01-01. See ?Date.
The Date object res can now be displayed as you please via
format(res, "%B-%Y")
#[1] "August-1973" "August-1974" "August-1975" "August-1976" "August-1977"
or similar.
The month(res) function and its cousins are also helpful. See ?month.

Resources