How to Calculate Date Difference in Years in R (Example Code)
In this R programming tutorial you’ll learn how to calculate the date difference in years.
Example Data
x1 <- as.Date("2033-11-10") # Example dates in R x1 # [1] "2033-11-10" |
x1 <- as.Date("2033-11-10") # Example dates in R x1 # [1] "2033-11-10"
x2 <- as.Date("2021-08-16") x2 # [1] "2021-08-16" |
x2 <- as.Date("2021-08-16") x2 # [1] "2021-08-16"
Example: Get Difference Between Dates in Years
install.packages("lubridate") # Install lubridate package library("lubridate") # Load lubridate |
install.packages("lubridate") # Install lubridate package library("lubridate") # Load lubridate
time_length(difftime(x1, x2), "years") # Calculate difference # [1] 12.23546 |
time_length(difftime(x1, x2), "years") # Calculate difference # [1] 12.23546
Further Resources & Related Tutorials
In the following, you can find some additional resources on topics such as vectors, data conversion, and numeric values.