0

I am calculating with dates in a For loop. I combine data from two dataframes. Tibble 1 contains variable A, tibble 2 contains variable B and C. A is a numerical variable, B and C are both dates.

I want to assign variable A a new variable if date B is within the interval of date C + 16 months.

I used the following:

if (B < C + months(16)) { Df1$A = A+1 }

For some dates this does not work. For example October 30th + 16 months = february 30th. The conditional expression fails as there is no true or falls and the for loop stops.

Is there a way to change C + months(16) to the last day of the month if the specific date (february 30th in the example above) does not exist?

WielsN
  • 1

1 Answers1

0

You can use %m+% to add 16 Months.

library(lubridate)
ymd('2000-10-30') %m+% months(16)
#[1] "2002-02-28"
GKi
  • 37,245
  • 2
  • 26
  • 48