gm0203
gm0203

Reputation: 45

Subtracting two dates in different formats in R

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!

Upvotes: 2

Views: 1678

Answers (4)

Blundering Ecologist
Blundering Ecologist

Reputation: 1315

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 

Upvotes: 0

maRtin
maRtin

Reputation: 6516

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()

Upvotes: 0

DaveH
DaveH

Reputation: 191

Use package lubridate to make date manipulation easy.

library(lubridate)  
somedate <- mdy("3/14/2015")  
today <- now()  
somedate - today  

Upvotes: 2

Max Candocia
Max Candocia

Reputation: 4386

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).

Upvotes: 0

Related Questions