I have the following date array
df$Date
[1] "2001-07-31" "2001-08-31" "2001-09-30" "2001-10-31" "2001-11-30" "2001-12-31" "2002-01-31" "2002-02-28"
[9] "2002-03-31" "2002-04-30" "2002-05-31" "2002-06-30" "2002-07-31" "2002-08-31" "2002-09-30" "2002-10-31"
[17] "2002-11-30" "2002-12-31" "2003-01-31" "2003-02-28" "2003-03-31" "2003-04-30" "2003-05-31" "2003-06-30"
[25] "2003-07-31" "2003-08-31" "2003-09-30" "2003-10-31" "2003-11-30" "2003-12-31" "2004-01-31" "2004-02-29"
[33] "2004-03-31" "2004-04-30" "2004-05-31" "2004-06-30" "2004-07-31" "2004-08-31" "2004-09-30" "2004-10-31"
[41] "2004-11-30" "2004-12-31" "2005-01-31" "2005-02-28" "2005-03-31" "2005-04-30" "2005-05-31" "2005-06-30"
[49] "2005-07-31" "2005-08-31" "2005-09-30" "2005-10-31" "2005-11-30" "2005-12-31" "2006-01-31" "2006-02-28"
[57] "2006-03-31" "2006-04-30" "2006-05-31" "2006-06-30" "2006-07-31" "2006-08-31" "2006-09-30" "2006-10-31"
[65] "2006-11-30" "2006-12-31" "2007-01-31" "2007-02-28" "2007-03-31" "2007-04-30" "2007-05-31" "2007-06-30"
[73] "2007-07-31" "2007-08-31" "2007-09-30" "2007-10-31" "2007-11-30" "2007-12-31" "2008-01-31" "2008-02-29"
[81] "2008-03-31" "2008-04-30" "2008-05-31" "2008-06-30" "2008-07-31" "2008-08-31" "2008-09-30" "2008-10-31"
[89] "2008-11-30" "2008-12-31" "2009-01-31" "2009-02-28" "2009-03-31" "2009-04-30" "2009-05-31" "2009-06-30"
[97] "2009-07-31" "2009-08-31" "2009-09-30" "2009-10-31" "2009-11-30" "2009-12-31" "2010-01-31" "2010-02-28"
[105] "2010-03-31" "2010-04-30" "2010-05-31" "2010-06-30" "2010-07-31" "2010-08-31" "2010-09-30" "2010-10-31"
[113] "2010-11-30" "2010-12-31" "2011-01-31" "2011-02-28" "2011-03-31" "2011-04-30" "2011-05-31" "2011-06-30"
[121] "2011-07-31" "2011-08-31" "2011-09-30" "2011-10-31" "2011-11-30" "2011-12-31" "2012-01-31" "2012-02-29"
[129] "2012-03-31" "2012-04-30" "2012-05-31" "2012-06-30" "2012-07-31" "2012-08-31" "2012-09-30" "2012-10-31"
[137] "2012-11-30" "2012-12-31" "2013-01-31" "2013-02-28" "2013-03-31" "2013-04-30" "2013-05-31" "2013-06-30"
[145] "2013-07-31" "2013-08-31" "2013-09-30" "2013-10-31" "2013-11-30" "2013-12-31" "2014-01-31" "2014-02-28"
[153] "2014-03-31" "2014-04-30"
I want to convert all those in a way that all start on the first day of each month:
for example: 2001-07-31
should become 2007-07-01
the 2013-08-28
should become 2013-08-01
and so on.
Can someone help me with this task?
To create a Date object from a simple character string in R, you can use the as. Date() function. The character string has to obey a format that can be defined using a set of symbols (the examples correspond to 13 January, 1982): %Y : 4-digit year (1982)
Lubridate is an R package that makes it easier to work with dates and times.
The first date of the month in R You can do it with the floor_date function from lubridate. If you have the first date of the month, then it is easy to calculate the last day of the previous month by simple subtraction, but it will work with the class of Date.
In R, dates are represented as the number of days since 1970-01-01. All the dates in R are internally stored in this way. Before we explore this concept further, let us learn to create Date objects in R.
Use rollback () if you want to change the date to the last day of the previous month or the first day of the month. To change the date to the first day of the month, use the roll_to_first argument and set it to TRUE. round up R release dates to hours round down R release dates to minutes
Use day () to extract the date component. There are other variations such as wday can return a number abbreviation of the weekday full name of the weekday ## [1] Thursday ## 7 Levels: Sunday < Monday < Tuesday < Wednesday < Thursday < ... < Saturday weekdays () from base R also returns the day of the week (the name and not the number).
Let’s get started… First, I’ll have to create some example data: As you can see based on the previous output of the RStudio console, our example data is a single date object (i.e. the 27th of May 2020). Note that we have used the as.Date function to create our example date.
We can use as.yearmon
from zoo
library(zoo)
as.Date(as.yearmon(df$Date), frac = 0)
#[1] "2001-07-01" "2001-08-01" "2001-09-01" "2001-10-01" "2001-11-01" "2001-12-01"
Or without using any external packages
as.Date(format(df$Date, "%Y-%m-01"))
df <- structure(list(Date = structure(c(11534, 11565, 11595, 11626,
11656, 11687), class = "Date")), row.names = c(NA, -6L),
class = "data.frame")
We can use floor_date
from lubridate
lubridate::floor_date(x, unit = "month")
#[1] "2001-07-01" "2001-08-01" "2001-09-01" "2001-10-01" "2001-11-01" "2001-12-01"
data
x <- as.Date(c("2001-07-31", "2001-08-31", "2001-09-30" ,"2001-10-31",
"2001-11-30", "2001-12-31"))
Non-canonical way(Then convert back to date):
as.Date(gsub("-\\d{2,}$","-01",df$Date))
#[1] "2001-07-01" "2001-08-01" "2001-09-01" "2001-10-01"
lubridate
has a great function for rounding dates:
library(lubridate)
floor_date(df$date, unit = "month")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With