代码之家  ›  专栏  ›  技术社区  ›  msh855

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

  •  3
  • msh855  · 技术社区  · 6 年前

    我有以下日期数组

    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 回复  |  直到 6 年前
        1
  •  5
  •   akrun    6 年前

    我们可以用 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")
    
        2
  •  3
  •   Ronak Shah    6 年前

    我们可以用 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"))
    
        3
  •  3
  •   NelsonGon phoxis    6 年前

    as.Date(gsub("-\\d{2,}$","-01",df$Date))
    #[1] "2001-07-01" "2001-08-01" "2001-09-01" "2001-10-01"
    
        4
  •  3
  •   OTStats Redfood    6 年前

    lubridate 具有很好的日期舍入功能:

    library(lubridate)
    floor_date(df$date, unit = "month")
    
        5
  •  2
  •   LocoGris    6 年前

    lubridate :

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