2

I'm trying to add a new column with date calculations. The calculation to be entered in the new column is variable MaturityDate minus today's date. MaturityDate in my dataset is in MM/DD/YYYY format and today's date entered with Sys.Date() is in a different format, which I think is what's giving me trouble when calculating. help please!

Tim McClure
  • 1,184
  • 2
  • 11
  • 24
gm0203
  • 45
  • 6
  • 2
    Make sure both values are proper POSIX date values in R (see `?DateTimeClasses`). Sounds right now like one of them may be a factor or string. But you really need to post a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data that closely resembles your own so we can see what's going on. – MrFlick May 06 '15 at 21:32

4 Answers4

2

Use package lubridate to make date manipulation easy.

library(lubridate)  
somedate <- mdy("3/14/2015")  
today <- now()  
somedate - today  
DaveH
  • 191
  • 5
0

I would convert the dates to a single format to be sure.

date.to.numeric <- function(x) as.numeric(strptime(x,'%m/%d/%Y'))
now <- function() as.numeric(strptime(Sys.Date(),'%Y-%m-%d'))

With this you get time difference in seconds using

date.to.numeric(date) - now()

Also look at as.POSIXct for more date formatting if you wanted something different (e.g., difference in calendar months).

Max Candocia
  • 4,294
  • 35
  • 58
0

When your dataframe is called YourDataFrame and your new column with the desired result should be called newCol:

YourDataFrame$newCol <- as.Date(MaturityDate, "%m/%d/%Y") - Sys.Date()
maRtin
  • 6,336
  • 11
  • 43
  • 66
0

Something that works well for me is using a combination of library(dplyr) and library(lubridate)like so:

dataset <-dataset %>% 
    mutate(MaturityDate=mdy(MaturityDate), #make sure to say what the format is first
    Sys.Date=mdy(Sys.Date)) %>%
    mutate(difference=as.numeric(difftime (Sys.Date, MaturityDate, units = "days")))

This gives something like so:

head(dataset,2)

> MaturityDate     Sys.Date     difference
> 2018-05-05      2018-05-26    50
> 2018-06-06      2018-06-10    48 
Blundering Ecologist
  • 1,199
  • 2
  • 14
  • 38