R subtraction 1 month from today gives NA

I have a script in which I will multiply my data in accordance with some set time periods and would like to multiply all the records that have occurred over the past month.

However, if I try to subtract the month from today, it will give NA:

> today <- Sys.Date()
> today
[1] "2017-03-29"
> today - months(1)
[1] NA

I have lubridate loaded, but I think this calculation is done with base R. If I subtract 2 or more months, it works fine:

> today - months(2)
[1] "2017-01-29"
> today - months(3)
[1] "2016-12-29"

Does anyone have any ideas on what might happen?

UPDATE: I think this involves simply subtracting the date without handling leap year cases (2017 is not a leap year, therefore "2017-02-29"does not exist).

/, ? , , :

today - months(1)
# Should yield:
"2017-02-28"

, , ( ISO)?

> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] xlsx_0.5.7         xlsxjars_0.6.1     rJava_0.9-8        MRAtools_0.6.8     stringdist_0.9.4.4 stringr_1.2.0     
 [7] stringi_1.1.3      lubridate_1.6.0    data.table_1.10.4  PKI_0.1-3          base64enc_0.1-3    digest_0.6.12     
[13] getPass_0.1-1      RPostgreSQL_0.5-1  DBI_0.5-1         

loaded via a namespace (and not attached):
[1] magrittr_1.5   rstudioapi_0.6 tools_3.3.2    parallel_3.3.2
+4
2

R, , . .

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

, %m+% lubridate:

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

. 29 .

lubridate :

. undefined, (, 29 ). . - % m +% add_with_rollback .

lubridate , , %m+%.

+2

Source: https://habr.com/ru/post/1673524/


All Articles