Reputation: 1571
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?
Upvotes: 5
Views: 201
Reputation: 1868
lubridate
has a great function for rounding dates:
library(lubridate)
floor_date(df$date, unit = "month")
Upvotes: 3
Reputation: 4480
Using lubridate
:
df$Date <- lubridate::ymd(df$Date)
df$Date <- df$Date-day(df$Date)+1
Upvotes: 2
Reputation: 887571
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")
Upvotes: 5
Reputation: 13319
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"
Upvotes: 3
Reputation: 389175
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"))
Upvotes: 3