![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
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"
Advertisements