- 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 add a month to a date in R?
We have to deal with date data in time series analysis, also sometimes we have a time variable in data set that is recorded to perform another type of analysis. Depending on our objective, we need to process the data and the time variable is also converted into appropriate form that we are looking for. If we want to create a sequence of months from date data then we can do it by adding a month to each upcoming month. This can be easily done by using AddMonths function of DescTools package.
Example
Installing DescTools package −
install.packages("DescTools") Loading DescTools package: library(DescTools) AddMonths(as.Date('2020/01/31'), 1) [1] "2020-02-29" AddMonths(as.Date('2020/01/31'), 2) [1] "2020-03-31" AddMonths(as.Date('2020/01/31'), 3) [1] "2020-04-30" AddMonths(as.Date('2020/01/31'), 4) [1] "2020-05-31" AddMonths(as.Date('2020/01/31'), 6) [1] "2020-07-31" AddMonths(as.Date('2020/01/01'), 6) [1] "2020-07-01" AddMonths(as.Date('2020/06/01'), 6) [1] "2020-12-01" AddMonths(as.Date('2020/06/30'), 6) [1] "2020-12-30" AddMonths(as.Date('2020/01/01'), 12) [1] "2021-01-01" AddMonths(as.Date('2020/01/01'), 24) [1] "2022-01-01" AddMonths(as.Date('2020/01/01'), 36) [1] "2023-01-01" AddMonths(as.Date('2020/01/01'), 48) [1] "2024-01-01" AddMonths(as.Date('2020/01/01'), 120) [1] "2030-01-01" AddMonths(as.Date('2021/01/01'), 120) [1] "2031-01-01" AddMonths(as.Date('2021/01/01'), 500) [1] "2062-09-01" AddMonths(as.Date('2021/01/01'), 600) [1] "2071-01-01" AddMonths(as.Date('2021/01/01'), 1200) [1] "2121-01-01" AddMonths(as.Date('2021-01-01'),8) [1] "2021-09-01" AddMonths(as.Date('2021-01-01'),10) [1] "2021-11-01" AddMonths(as.Date('2021-01-01'),20) [1] "2022-09-01" AddMonths(as.Date('2021-01-01'),25) [1] "2023-02-01" AddMonths(as.Date('2021-01-01'),16) [1] "2022-05-01"
- Related Articles
- How to convert year, month, and day of the month into a complete date in R?
- How to generate a sequence of a date in each month for a fixed number of months using R?
- How to add months to a date in JavaScript?
- How to convert a date or date vector to POSIXct in R?
- How to add a day to the date in MySQL?
- How to add a date and time in HTML5?
- How to get a Date from year, month and day in Java?
- How to filter a specific month in MySQL when date is in varchar?
- How to add 1 day to a Date in iOS/iPhone?
- How to Add or Subtract Weeks to a Date in Excel?
- How to add a variable description in R?
- How to add a new column to a matrix in R?
- How to add 30 minutes to a JavaScript Date object?
- How to add 2 hours to a JavaScript Date object?
- How to add 10 seconds to a JavaScript date object?

Advertisements