0

I have two coloumns START and DATE, I need to calculate the difference between the dates (in year, month and days) and then calculate average of the differences mean (START-END). Thanks for the help. This question is not the same to other questions which have been already posted before

Data:

start           end         diff
"2020-5-16"    "2029-7-15"    9 years, 2 months and etc
"2024-5-16"    "2028-3-13"
"2023-4-17"    "2025-4-12"
"2026-7-18"    "2028-5-16"

 star<-c("2020-5-16" ,  "2024-5-16" , "2023-4-17" ,  "2026-7-18")
 end<-c( "2029-7-15", "2028-3-13","2025-4-12","2028-5-16")
 data<-data.frame(star,end)
ElR
  • 17
  • 4
  • 2
    That is quite a complicated question: Different months have different lengths so "3 years, two months, 2 days" is not really a well defined time period. Should we imply January or Feburary length for that answer? – Bernhard Feb 17 '23 at 13:19
  • @ Bernhard, thanks for the reply, Unfortunately I could not get your question. Do I need to add more info into the data? – ElR Feb 17 '23 at 13:42

1 Answers1

1

You can use lubridate::as.period:

library(lubridate)
star<-c("2020-5-16" ,  "2024-5-16" , "2023-4-17" ,  "2026-7-18")
end<-c( "2029-7-15", "2028-3-13","2025-4-12","2028-5-16")
as.period(interval(as.Date(star), as.Date(end)))
#[1] "9y 1m 29d 0H 0M 0S"  "3y 9m 26d 0H 0M 0S"  "1y 11m 26d 0H 0M 0S" "1y 9m 28d 0H 0M 0S" 
Maël
  • 45,206
  • 3
  • 29
  • 67
  • You can use the difftime() function to obtain the difference between the two dates in seconds.using the lubridate package in R language – MD Shahid Khan Feb 17 '23 at 13:38
  • @ MD Shahid Khan, thanks for the reply.difftime() function just gives the results in either years, or months, or days. I need to have the difference in (forexample: 3 years, two months, 1 day) and then calculate the average. – ElR Feb 17 '23 at 13:53
  • @Maël, thanks so much for your help. Just one question. I need to calculate the average of all these differences. I have two variables, START and END and after calculating the differences between dates i need to calculate the average. I was wondering how I can do it. – ElR Feb 17 '23 at 14:00
  • `mapply(function(x, y) as.period(interval(x, y)), START, END)` – Maël Feb 17 '23 at 14:15
  • @ Maël, thanks so much for your time, but this function doesnot work correcly. After calculating the diff column I need to calculate the average of diff. Could you please check the question and modify your code. – ElR Feb 17 '23 at 14:23
  • see edit, interval is vectorized so no problem – Maël Feb 17 '23 at 14:34
  • @ Maël, thanks but i need to calcuate the average of those differences. I mean sth like mean(as.period(interval(as.Date(star), as.Date(end)))) – ElR Feb 17 '23 at 14:44
  • You can do `as.period(mean(interval(as.Date(star), as.Date(end))))` but you'll get seconds. Withtout reference point, you can't compute periods because years have different lenghts – Maël Feb 17 '23 at 14:59