R subtracting 1 month from today's date gives NA

前端 未结 2 1487
北恋
北恋 2020-12-11 16:36

I have a script in which I subset my data according to some set time periods and wanted to subset all the records that had occurred in the last month.

However if I t

2条回答
  •  庸人自扰
    2020-12-11 17:13

    The calculation of months is indeed perfomed by base R but not the way your think. Months is used to get the month of a date object.

    #Example
    today <- Sys.Date()
    months(today)
    [1] "March"
    

    To add or substract months, you should use %m+% from lubridate:

    today <- Sys.Date()
    today %m+% months(-1)
    [1] "2017-02-28"
    

提交回复
热议问题