How to calculate monthly average for time series object in R?


To calculate monthly average for time series object, we can use tapply function with mean. For example, if we have a time series object called TimeData then the monthly average for this series can be found by using the command tapply(TimeData,cycle(TimeData),mean).

Example1

Consider the below time series object −

Live Demo

> Data1<-ts(sample(101:999,240),frequency=12)
> Data1

Output

   Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1  988 695 867 211 915 348 729 518 592 447 448 880
2  551 410 427 134 133 572 637 800 630 878 642 940
3  603 335 638 639 595 512 671 863 752 568 608 669
4  719 899 297 399 252 890 474 723 326 896 618 712
5  146 505 983 614 117 190 274 769 237 667 803 673
6  428 611 873 303 656 585 917 886 855 385 949 131
7  905 977 110 264 941 699 242 559 251 676 895 194
8  520 528 966 435 891 213 508 288 381 137 197 200
9  141 678 477 219 795 900 339 869 538 820 174 986
10 979 406 583 527 590 945 626 791 191 842 423 963
11 982 997 650 396 445 401 881 647 575 491 989 525
12 353 467 951 546 654 919 609 797 314 798 269 497
13 541 236 556 488 459 696 934 415 883 124 943 522
14 904 108 565 858 285 347 526 833 815 312 567 187
15 952 180 153 720 203 838 244 250 871 930 810 761
16 248 223 432 737 834 875 971 454 444 563 493 739
17 909 249 144 648 400 404 768 120 975 216 132 489
18 179 968 193 471 284 974 931 617 463 543 103 596
19 470 756 516 422 787 356 674 519 469 547 765 996
20 165 924 751 515 426 874 722 682 393 479 732 634

Finding monthly average for data in Data1 −

> tapply(Data1,cycle(Data1),mean)

Output

     1      2      3      4      5      6      7      8      9     10     11      12
584.15 557.60 556.60 477.30 533.10 626.90 633.85 620.00 547.75 565.95 578.00   614.70

Example2

Live Demo

> Data2<-ts(sample(101:999,360),frequency=12)
> Data2

Output

   Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1  122 308 708 632 489 331 160 537 245 856 375 560
2  511 862 765 969 118 401 735 226 305 540 812 435
3  135 461 488 567 283 991 260 996 753 803 858 696
4  384 197 171 175 515 697 600 821 884 928 570 939
5  599 929 288 227 749 962 155 723 569 377 572 888
6  948 810 163 186 213 950 634 304 785 278 897 418
7  711 687 952 174 123 348 467 114 624 825 802 642
8  399 786 719 516 701 681 121 417 868 584 436 787
9  561 647 194 554 199 146 956 731 879 477 520 474
10 404 367 425 127 911 823 974 918 462 466 926 212
11 668 797 747 125 608 876 877 814 763 585 851 360
12 350 140 596 450 597 981 578 690 338 496 666 519
13 660 933 538 827 397 800 583 551 699 684 321 185
14 475 618 387 130 513 107 703 207 781 857 221 915
15 494 984 349 225 558 993 336 541 364 620 649 352
16 581 170 303 361 383 104 145 138 654 240 975 222
17 913 864 693 658 622 846 661 872 686 188 318 779
18 297 886 253 890 209 688 745 980 281 522 645 428
19 799 921 129 631 629 476 755 630 454 590 192 945
20 355 242 822 777 853 685 270 173 670 369 586 971
21 807 836 555 767 106 593 351 961 783 485 626 263
22 871 131 344 158 674 433 751 582 426 893 137 953
23 577 259 365 998 376 573 481 677 307 989 248 395
24 714 835 415 232 150 441 285 667 480 438 579 157
25 294 588 664 315 710 659 530 970 831 301 169 182
26 116 113 458 695 899 916 445 758 552 808 683 354
27 329 923 176 165 437 464 493 409 431 938 752 707
28 506 689 517 951 847 587 495 429 775 447 840 833
29 705 162 883 472 220 985 901 869 637 478 992 359
30 602 324 191 440 491 411 729 598 358 370 393 609

Finding monthly average for data in Data2 −

> tapply(Data2,cycle(Data2),mean)

Output

       1        2        3        4        5        6        7        8
529.5667 582.3000 475.2667 479.8000 486.0000 631.6000 539.2000 610.1000
       9       10       11       12
588.1333 581.0667 587.0333 551.3000

Updated on: 06-Mar-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements