How to find the difference in number of days between two date columns of an R data frame?

R ProgrammingServer Side ProgrammingProgramming

When dealing with date data, we often want to find the difference between dates if the data contains two or more date values. Same thing can be done for the two columns of an R data frame that contains dates but first we need to read those date columns in date format in case they are not recorded as date in R. The finding of difference in number of days can be done by using difftime function.

Example

Consider the below data −

date1<-c("2020/02/02","2020/02/04","2020/02/05","2020/02/06","2020/02/08","2020/02/12","2020/02/13","2020/02/15","2020/02/16","2020/02/17","2020/02/18","2020/02/19","2020/02/20","2020/02/22","2020/02/23","2020/02/24","2020/02/25","2020/02/26","2020/02/27","2020/03/01")
date1
[1] "2020/02/02" "2020/02/04" "2020/02/05" "2020/02/06" "2020/02/08"
[6] "2020/02/12" "2020/02/13" "2020/02/15" "2020/02/16" "2020/02/17"
[11] "2020/02/18" "2020/02/19" "2020/02/20" "2020/02/22" "2020/02/23"
[16] "2020/02/24" "2020/02/25" "2020/02/26" "2020/02/27" "2020/03/01"
date2<-c("2020/03/02","2020/03/03","2020/03/05","2020/03/06","2020/03/07","2020/03/08","2020/03/10","2020/03/13","2020/03/14","2020/03/15","2020/03/16","2020/03/17","2020/03/18","2020/03/20","2020/03/21","2020/03/22","2020/03/23","2020/03/24","2020/03/26","2020/03/27")
date2
[1] "2020/03/02" "2020/03/03" "2020/03/05" "2020/03/06" "2020/03/07"
[6] "2020/03/08" "2020/03/10" "2020/03/13" "2020/03/14" "2020/03/15"
[11] "2020/03/16" "2020/03/17" "2020/03/18" "2020/03/20" "2020/03/21"
[16] "2020/03/22" "2020/03/23" "2020/03/24" "2020/03/26" "2020/03/27"
df<-data.frame(date1,date2)
df

Output

date1 date2
1 2020/02/02 2020/03/02
2 2020/02/04 2020/03/03
3 2020/02/05 2020/03/05
4 2020/02/06 2020/03/06
5 2020/02/08 2020/03/07
6 2020/02/12 2020/03/08
7 2020/02/13 2020/03/10
8 2020/02/15 2020/03/13
9 2020/02/16 2020/03/14
10 2020/02/17 2020/03/15
11 2020/02/18 2020/03/16
12 2020/02/19 2020/03/17
13 2020/02/20 2020/03/18
14 2020/02/22 2020/03/20
15 2020/02/23 2020/03/21
16 2020/02/24 2020/03/22
17 2020/02/25 2020/03/23
18 2020/02/26 2020/03/24
19 2020/02/27 2020/03/26
20 2020/03/01 2020/03/27

Changing the format of date1 and date2 to date format −

Example

df$date1<-as.Date(df$date1,format="%Y/%m/%d")
df$date2<-as.Date(df$date2,format="%Y/%m/%d")

Finding the difference between two dates in number of days −

Example

df$Date_difference_in_days <-difftime(df$date1,df$date2,units=c("days"))
df

Output

date1 date2 Date_difference_in_days
1 2020-02-02 2020-03-02 -29 days
2 2020-02-04 2020-03-03 -28 days
3 2020-02-05 2020-03-05 -29 days
4 2020-02-06 2020-03-06 -29 days
5 2020-02-08 2020-03-07 -28 days
6 2020-02-12 2020-03-08 -25 days
7 2020-02-13 2020-03-10 -26 days
8 2020-02-15 2020-03-13 -27 days
9 2020-02-16 2020-03-14 -27 days
10 2020-02-17 2020-03-15 -27 days
11 2020-02-18 2020-03-16 -27 days
12 2020-02-19 2020-03-17 -27 days
13 2020-02-20 2020-03-18 -27 days
14 2020-02-22 2020-03-20 -27 days
15 2020-02-23 2020-03-21 -27 days
16 2020-02-24 2020-03-22 -27 days
17 2020-02-25 2020-03-23 -27 days
18 2020-02-26 2020-03-24 -27 days
19 2020-02-27 2020-03-26 -28 days
20 2020-03-01 2020-03-27 -26 days
raja
Published on 24-Aug-2020 11:54:38
Advertisements