Substract two columns with dates

Hello,

I have a two-column table. One has rows with dates, in this format: 2022-01-24 for example. The other has a number of days (ex: 3).

I would like to add another column with for each row a date obtained by subtracting the date in the first column and the number of days of the second one.
(for example, 2022-01-24 - 3 = 2022-01-21 is the date I want in my new column)

I have been trying to do simple calculations (and change the format of the date) but it does not work well.

What have you tried that fell short ?

Here is a simply example of what you describe.


(exdf <- data.frame(
  d1=seq.Date(Sys.Date()-9,Sys.Date(),by=1),
  days=seq(1,by=3,length.out=10)
))

exdf$newdata <- exdf$d1 - exdf$days

exdf

Thank you for your answer.

This is actually what I did but I obtain this message:

Error in unclass(e1) - e2 : non-numeric argument to binary operator

EDIT: just needed to add 'as.numeric()', thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.