Search code examples
rdate-differencedifftime

R - Difference Dates in new Row


I'm not a pro in R and maybe someone can help me with this function. There is a merged Dataframe (Analyse) with 2 rows (2 different Dates) and I need a new row with the difference between the two dates (in days). I tried it with difftime but the new row stays empty.

Date 1      Date 2     Days
2015-09-20  2015-09-21   1
2015-09-12  2015-09-15   3

my code:

Analyse$Date1 <- as.POSIXlt(Analyse$Date1)
Analyse$Date2<- as.POSIXlt(Analyse$Date2)
Analyse$Days <- difftime( Analyse$Date2, Analyse$Date2, units = c("days") )

Solution

  • is there a typo in your code for Analyse$Days - did you mean the difference between col Date1 and Column Date2:

    Analyse$Days <- difftime( Analyse$Date1, Analyse$Date2, units = c("days") )