Date format years-months in R [duplicate] - r

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 9 years ago.
I have a data in the following format 200101 and I want it to be in the following format 2001-01 or 2001/01
Thanks

I don't deal with dates so there may well be better approaches. Your problem is you have no day. I know the zoo package can handle this but not in the format you want. I also give a regex approach but this is not a date class, just character.
As date:
library(zoo)
as.yearmon("200101", "%Y%m")
## > as.yearmon("200101", "%Y%m")
## [1] "Jan 2001"
As character:
gsub("([0-9]{4})","\\1-", "200101")
## > gsub("([0-9]{4})","\\1-", "200101")
## [1] "2001-01"
## gsub("([0-9]{4})","\\1/", "200101")

Related

R Convert concatenated date in chr format to dd-mm-yyyy date format [duplicate]

This question already has answers here:
Mysterious error by parsing French dates on OSX
(4 answers)
Closed 11 months ago.
I've got a date in character format (month in French) that looks like this -
date = "30juillet2021"
I'd like to convert it to dd-mm-yyyy date format
Desired output
30/07/2021
I've tried a number of solutions on stackoverflow, but none seem to work.
Any help would be appreciated
Sys.setlocale('LC_TIME', "French_France")
[1] "French_France.1252"
as.Date(date, "%d%B%Y")
[1] "2021-07-30"
format(as.Date(date, "%d%B%Y"), "%d/%m/%Y")
[1] "30/07/2021"

How to convert character into date format? [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 2 years ago.
I've got a character like this:
"2008-11" and I want to convert it in a date format. (only year and month)
I've already tried with zoo package:
yearmon("2008-11")
but it returns a NUM. I want a Date as structure. It should return 2008-11.
Which is the fastest way?
Thanks
library(anytime)
a <- anydate("2008-11")
> class(a)
[1] "Date"
> a
[1] "2008-11-01"

Convert character date to date format [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 2 years ago.
I have date in character vector. I need to convert it to date format.
For example :
Month = "202005"
Output should be May-20. It should be in date format so that I can use to filter some dates. Similarly 201912 should return Dec-19
zoo has a function called yearmon().
library(zoo)
month <- "202005"
as.yearmon(month, "%Y%m")
# "May 2020"

Converting dates with a format of d/mm/yy (for years earlier than 69) [duplicate]

This question already has answers here:
Add correct century to dates with year provided as "Year without century", %y
(3 answers)
Closed 9 years ago.
Converting the character "6/07/69" into a date using the as.Date function results in "2068-07-06" instead of "1968-07-06". How can I fix this?
Example:
as.Date(c("6/07/68", "6/07/69"), format="%d/%m/%y")
[1] "2068-07-06" "1969-07-06"
you can use library chron
e.g.
> library(chron)
> as.Date(chron(c("6/07/67", "6/07/69"), format = c(dates = "m/d/y")))
#[1] "1967-06-07" "1969-06-07"

date format in R [duplicate]

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")

Resources