How to change the date format from "year/month/day" to "year/month" in R?

Asked

Viewed 1,547 times

1

Suppose I have a date in the standard English format "ymd", example:

Sys.Date()

"2019-04-11"

Since for my analysis I do not need this date to have the days and I decide to remove them, so that it has a simpler format: "2019-04".

How do I change the date format from "year/month/day" to "year/month only"?

1 answer

3


Here are two different ways to convert a date to "ano/mês".

R base.
Use the method format.Date for class objects "Date".

format(Sys.Date(), "%Y-%m")
#[1] "2019-04"

Bundle zoo.
The function as.yearmon does just that.

zoo::as.yearmon(Sys.Date())
#[1] "abr 2019"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.