I have a column with end dates and another column with number of days. I want to calculate start date from this information.
End dates: 7/15/2019; 6/10/2019
Number of days: 10; 9
I want to calculate Start date from the above information in R.
I have a column with end dates and another column with number of days. I want to calculate start date from this information.
End dates: 7/15/2019; 6/10/2019
Number of days: 10; 9
I want to calculate Start date from the above information in R.
The lubridate
package provides functions to convert strings to Date
(in this case, mdy
). Then you can just subtract the number of days from the end date.
ed = c("7/15/2019", "6/10/2019")
n = c(10, 9)
lubridate::mdy(ed) - n
#[1] "2019-07-05" "2019-06-01"
Consider straightforward subtraction with base R:
df <- within(df, {
end_date <- as.Date(end_date, format="%m/%d/%Y")
start_date <- end_date - num_of_days
})