使日期从R中的同一天开始

时间:2019-04-13 14:00:15

标签: r date type-conversion

我有以下日期数组

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"

我想将所有这些转换为每个月的第一天开始:

例如:2001-07-31应该变成2007-07-01 2013-08-28应该变成2013-08-01,依此类推。

有人可以帮助我完成此任务吗?

5 个答案:

答案 0 :(得分:5)

我们可以使用as.yearmon中的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"

或者不使用任何外部软件包

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

答案 1 :(得分:3)

我们可以使用floor_date中的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"

数据

x <- as.Date(c("2001-07-31", "2001-08-31", "2001-09-30" ,"2001-10-31", 
              "2001-11-30", "2001-12-31"))

答案 2 :(得分:3)

lubridate具有很好的四舍五入功能:

library(lubridate)
floor_date(df$date, unit = "month")

答案 3 :(得分:3)

非规范方法(然后转换回日期):

as.Date(gsub("-\\d{2,}$","-01",df$Date))
#[1] "2001-07-01" "2001-08-01" "2001-09-01" "2001-10-01"

答案 4 :(得分:2)

使用lubridate

df$Date <- lubridate::ymd(df$Date)
df$Date <- df$Date-day(df$Date)+1