0

I have a sample dataset as the one below:

df= data.frame("Finish_Date"=c("10/2019", "07/2005", "09/2008"), 
               "Recent_Date"="01/2020", stringsAsFactors = F)

I need to create a new column and calculate the difference between these two dates such that I get the difference in terms of "months". I have tried the solution from this post Get the difference between dates in terms of weeks, months, quarters, and years, however, I'm getting NA's in my result.

Below is the code that I have tried:

df$Due_Date=(as.yearmon(df$Recent_Date, format = "%Y-%m")-
                  as.yearmon(df$Finish_Date, format = "%Y-%m"))*12

My final output is supposed to be like:

| Finish_Date | Recent_Date | Due_Date |
|-------------|:-----------:|---------:|
| 10/2019     |   01/2020   |        3 |
| 07/2005     |   01/2020   |      174 |
| 09/2008     |   01/2020   |      136 |

Is there any way to get the result?

hk2
  • 487
  • 3
  • 15
  • 1
    This is essentially a typo. Your date format has to match your strings; otherwise, you've told R to look for information in one way, but then given it in another, getting NA. This is why it's good to debug in steps—you should have seen that you have NA at the point of `as.yearmon`, before even doing any difference calculations, because you gave the wrong formatting – camille Jan 30 '20 at 17:41

1 Answers1

1

We need to provide the correct format. Here, it is %m/%Y instead of %Y-%m

library(zoo)
df$Due_Date <- (as.yearmon(df$Recent_Date, format = "%m/%Y")-
               as.yearmon(df$Finish_Date, format = "%m/%Y"))*12
df$Due_Date
#[1]   3 174 136
akrun
  • 874,273
  • 37
  • 540
  • 662