How to calculate Years between Dates in R ?
Last Updated :
25 Sep, 2023
To calculate the number of years between two dates we have several methods. In this article, we will discuss all the methods and their examples of how to calculate the number of years or the difference of years between two dates in the R Programming Language.
Example:
Input:
Date_1 = 2020/02/21
Date_2 = 2023/03/21
Output:
3
Explanation:
In Date_1 and Date_2 have three years difference in year.
Here we will use the seq() function to get the result. This function is used to create a sequence of elements in a Vector.
Syntax:
length(seq(from=date_1, to=date_2, by=’year’)) -1
Method 1: Using the seq() function
Example 1:
R
date_1<- as.Date ( "2020-08-10" )
date_2<- as.Date ( "2023-10-10" )
a = seq (from = date_1, to = date_2, by = 'year' )
print (a)
print ( length (a)-1)
|
Output:
[1] "2020-08-10" "2021-08-10" "2022-08-10" "2023-08-10"
[1] 3
Example 2:
R
date_1<- as.Date ( "2020-08-10" )
date_2<- as.Date ( "2100-08-10" )
a = seq (from = date_1, to = date_2, by = 'year' )
print (a)
print ( length (a)-1)
|
Output:
[1] "2020-08-10" "2021-08-10" "2022-08-10" "2023-08-10" "2024-08-10"
[6] "2025-08-10" "2026-08-10" "2027-08-10" "2028-08-10" "2029-08-10"
[11] "2030-08-10" "2031-08-10" "2032-08-10" "2033-08-10" "2034-08-10"
[16] "2035-08-10" "2036-08-10" "2037-08-10" "2038-08-10" "2039-08-10"
[21] "2040-08-10" "2041-08-10" "2042-08-10" "2043-08-10" "2044-08-10"
[26] "2045-08-10" "2046-08-10" "2047-08-10" "2048-08-10" "2049-08-10"
[31] "2050-08-10" "2051-08-10" "2052-08-10" "2053-08-10" "2054-08-10"
[36] "2055-08-10" "2056-08-10" "2057-08-10" "2058-08-10" "2059-08-10"
[41] "2060-08-10" "2061-08-10" "2062-08-10" "2063-08-10" "2064-08-10"
[46] "2065-08-10" "2066-08-10" "2067-08-10" "2068-08-10" "2069-08-10"
[51] "2070-08-10" "2071-08-10" "2072-08-10" "2073-08-10" "2074-08-10"
[56] "2075-08-10" "2076-08-10" "2077-08-10" "2078-08-10" "2079-08-10"
[61] "2080-08-10" "2081-08-10" "2082-08-10" "2083-08-10" "2084-08-10"
[66] "2085-08-10" "2086-08-10" "2087-08-10" "2088-08-10" "2089-08-10"
[71] "2090-08-10" "2091-08-10" "2092-08-10" "2093-08-10" "2094-08-10"
[76] "2095-08-10" "2096-08-10" "2097-08-10" "2098-08-10" "2099-08-10"
[81] "2100-08-10"
[1] 80
Method 2: Using the difftime
function
R
date1 <- as.Date ( "2021-01-01" )
date2 <- as.Date ( "2023-08-01" )
weeks_diff <- difftime (date2, date1, units = "weeks" )
print (weeks_diff)
year_diff <- weeks_diff / 52
print ( paste ( 'Year difference:' , year_diff))
days_diff <- difftime (date2, date1, units = "days" )
print (days_diff)
year_diff <- days_diff / 365
print ( paste ( 'Year difference:' , year_diff))
|
Output:
Time difference of 134.5714 weeks
[1] "Year difference: 2.58791208791209"
Time difference of 942 days
[1] "Year difference: 2.58082191780822"
Method 3: Using the base R as.POSIXlt
function
R
date1 <- as.POSIXlt ( "2021-01-01" )
date2 <- as.POSIXlt ( "2023-08-01" )
years_diff <- date2$year - date1$year
years_diff
|
Output:
[1] 2
methods
Share your thoughts in the comments
Please Login to comment...