- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the number of days and number of weeks between two dates in R?
In data analysis, time series is one of the common data we have to deal with and it might also contain dates data along with other variables. We might want to find the difference between two times to check how many days or weeks have changed the time series. This can be easily done with the help of difftime function.
Example
> difftime(strptime("25/07/2021", format = "%d/%m/%Y"), + strptime("25/07/2020", format = "%d/%m/%Y"),units="weeks") Time difference of 52.14286 weeks > difftime(strptime("25.07.2021", format = "%d.%m.%Y"), + strptime("25.07.2020", format = "%d.%m.%Y"),units="weeks") Time difference of 52.14286 weeks > difftime(strptime("25.07.2021", format = "%d.%m.%Y"), + strptime("25.07.2020", format = "%d.%m.%Y")) Time difference of 365 days > difftime(strptime("01.01.2021", format = "%d.%m.%Y"), + strptime("25.07.2020", format = "%d.%m.%Y")) Time difference of 160 days > difftime(strptime("01.01.2021", format = "%d.%m.%Y"), + strptime("25.07.2020", format = "%d.%m.%Y"),units="weeks") Time difference of 22.85714 weeks > difftime(strptime("01.01.2050", format = "%d.%m.%Y"), + strptime("01.01.2020", format = "%d.%m.%Y")) Time difference of 10958 days > difftime(strptime("01.01.2050", format = "%d.%m.%Y"), + strptime("01.01.2020", format = "%d.%m.%Y"),units="weeks") Time difference of 1565.429 weeks > difftime(strptime("01.01.2030", format = "%d.%m.%Y"), + strptime("01.01.2020", format = "%d.%m.%Y"),units="weeks") Time difference of 521.8571 weeks > difftime(strptime("01.01.2030", format = "%d.%m.%Y"), + strptime("01.01.2020", format = "%d.%m.%Y")) Time difference of 3653 days > difftime(strptime("25/12/2021", format = "%d/%m/%Y"), + strptime("01/01/2021", format = "%d/%m/%Y"),units="weeks") Time difference of 51.14286 weeks > difftime(strptime("25/12/2021", format = "%d/%m/%Y"), + strptime("01/01/2021", format = "%d/%m/%Y")) Time difference of 358 days > difftime(strptime("01/01/3000", format = "%d/%m/%Y"), + strptime("01/01/2000", format = "%d/%m/%Y")) Time difference of 365243 days > difftime(strptime("01/01/3000", format = "%d/%m/%Y"), + strptime("01/01/2000", format = "%d/%m/%Y"),units="weeks") Time difference of 52177.57 weeks > difftime(strptime("01/01/0001", format = "%d/%m/%Y"), + strptime("25/07/2020", format = "%d/%m/%Y")) Time difference of -737630 days > difftime(strptime("01/01/0001", format = "%d/%m/%Y"), + strptime("25/07/2020", format = "%d/%m/%Y"),units="weeks") Time difference of -105375.7 weeks
- Related Articles
- How to calculate the weeks and days between two dates in Excel?
- Find number of days between two given dates in C++
- How to get the number of days between two Dates in JavaScript?
- Python program to find number of days between two given dates
- Finding Number of Days Between Two Dates JavaScript
- How do I get the number of days between two dates in JavaScript?
- Calculate the difference between two dates in days, weeks, months and years in Excel
- How do I calculate number of days between two dates using Python?
- Calculating total number of Hours, Days, Minutes and Seconds between two dates in Golang
- Find the number of logins between two dates in MySQL
- How to find the difference in number of days between two date columns of an R data frame?
- Program to convert given number of days in terms of Years, Weeks and Days in C
- How to get the number of seconds between two Dates in JavaScript?
- How to get number of quarters between two dates in Java
- How to find the date after a number of days in R?

Advertisements