0

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") )
Kevin Arseneau
  • 6,186
  • 1
  • 21
  • 40
AO_30
  • 39
  • 5
  • 2
    Welcome to R! You are *replacing a column*, not *adding a row*. I suggest you find tutorials on R data management, as this functionality is very basic to the language. – r2evans Jan 02 '18 at 00:53
  • 1
    There are multiple problems with your code,The column name is "Date 1" (Date and a then 1), however when you calling it , you are using `$` with Date1 (without spaces), also when code is called, Date2 is being used twice(it should be both Date 1 and Date 2), The difference will always be zero. Not going to work. Also I believe you want to create a column with two rows having difference of dates. you may try: `difftime( Analyse[,"Date 1"], Analyse[,"Date 2"], units = c("days") )`. – PKumar Jan 02 '18 at 05:54
  • Thank you, Im sorry for the mistake, I write the code by hand, instead of copy it, there was no space between in the column name Thank you for your help! – AO_30 Jan 02 '18 at 10:13

1 Answers1

0

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") )
PKumar
  • 10,971
  • 6
  • 37
  • 52
coding_is_fun
  • 117
  • 1
  • 14
  • yes, I tried some changes and forget to change it back, thank you, your code helped me: **difftime( Analyse[,"Date 1"], Analyse[,"Date 2"], units = c("days") )** I'm impressed how fast I get the right help :) – AO_30 Jan 02 '18 at 10:30