Convert character date to date format [duplicate] - r

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"

Related

How to change from character to date format? [duplicate]

This question already has answers here:
Convert string to date, format: "dd.mm.yyyy"
(4 answers)
What are the "standard unambiguous date" formats for string-to-date conversion in R?
(8 answers)
Closed 3 months ago.
For my dataset, the Date variable has dates in the format of this example: 19-Feb-03
I want to change the above character format dates in the column to a Date format. (As I have to do time series analysis later on)
I tried using the as.Date() method but it didn't work.
x <- '19-Feb-03'
lubridate::ymd(x)
"2019-02-03"
Not sure whether 19 is year or day. You can try lubridate package
x<-"19-Feb-03"
library(lubridate)
ymd(x)
dmy(x)

Convert character into date format [duplicate]

This question already has answers here:
Convert a yyyymm format to date in R? [duplicate]
(1 answer)
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 8 months ago.
I currently have a database with dates in character format yyyy-mm format. e.g. 2009-01 and missing values.
I'm struggling to convert this into a date format.
You could use ym from lubridate package:
lubridate::ym('2009-01')
#[1] "2009-01-01"

How to format a AAAAMMDD in date AAAA-MM-DD IN R? [duplicate]

This question already has answers here:
Convert integer to class Date
(3 answers)
Closed 2 years ago.
I have a column with date in the format 20201020 (AAAAMMDD) as integer and a need to format as date 2020-10-20.
I`ve tried:
as.Date(MYDATE, format = "%m/%d/%y", origin = "1900-01-01")
AND
format(MYDATE, "%m/%d/%Y")
But returned just
NA
Using lubridate we can:
ymd(MY_DATE)

Convert integer data into year and month [duplicate]

This question already has answers here:
Convert numeric date format (YYYYMMDD; 20150101) into Year-Month
(3 answers)
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 2 years ago.
I have integer data example like this
201901, 201912, 202004
How to convert my data into year and month using R Programming. I need output like this:
2019-01, 2019-12, 2020-04
or
Jan-2019, Dec-2019, Apr-2020
Thank you in advance.
You can convert to date and then use format :
x <- c(201901, 201912, 202004)
format(as.Date(paste(x, '01'), '%Y%m%d'), '%b-%Y')
#[1] "Jan-2019" "Dec-2019" "Apr-2020"
format(as.Date(paste(x, '01'), '%Y%m%d'), '%Y-%m')
#[1] "2019-01" "2019-12" "2020-04"
You can also use zoo's yearmon :
zoo::as.yearmon(as.character(x), '%Y%m')
Using regex we can capture 4 and 2 characters and insert a "-" in between.
sub('(.{4})(.{2})', '\\1-\\2', x)

How to convert mmm-yy(Jan-85) date format into yyyy-mm--dd (1985-01-02)date format in R? [duplicate]

This question already has an answer here:
format year-month to POSIXct [duplicate]
(1 answer)
Closed 3 years ago.
I want to convert mmm-yy(Jan-85) date format into yyyy-mm-dd (1985-01-02)date format in R.
One option is to paste the day and convert it to Date class with as.Date and the appropriate format
as.Date(paste0("Jan-85", "-02"), "%b-%y-%d")
#[1] "1985-01-02"

Resources