0
votes

I have a dataframe which has 23 columns which first column is belong to date and time together.

>str(combined)

'data.frame':   100082 obs. of  23 variables:

$ date: POSIXlt, format: "2004-01-01 01:00:00" "2004-01-01 03:00:00" "2004-01-01 04:00:00" "2004-01-01 05:00:00" ...
 $ t1  : num  665 556 714 890 878 ...
 $ t2  : num  2374 2238 2336 2358 2331 ...
 $ t3  : num  2302 2091 2238 2272 2218 ...
 $ t4  : num  2400 2400 2400 2400 2400 2400 2400 2400 2400 2400 ...
 $ t5  : num  2400 2400 2400 2400 2400 2400 2400 2400 2400 2400 ...
 $ t6  : num  2400 2400 2400 2400 2400 ...
 $ t7  : num  2400 2400 2400 2400 2400 ...
 $ t8  : num  2400 2400 2400 2400 2400 ...
 $ t9  : num  2111 1843 1916 1920 1907 ...
 $ t10 : num  1834 1645 1762 1777 1751 ...
 $ t11 : num  1608 1518 1662 1687 1652 ...
 $ t12 : num  626 529 675 842 834 ...
 $ t13 : num  1766 1706 1866 1888 1852 ...
 $ t14 : num  1957 1792 1907 1916 1893 ...
 $ t15 : num  2034 1938 2068 2085 2059 ...
 $ t16 : num  497 417 538 675 668 ...
 $ t17 : num  445 364 473 591 579 ...
 $ t18 : num  376 268 358 435 417 ...
 $ t19 : num  597 473 608 709 658 ...
 $ t20 : num  638 500 641 742 676 ...
 $ t21 : num  2387 2263 2345 2371 2350 ...
 $ t22 : num  2390 2291 2363 2385 2362 ...

Date is going through the 2017 - 08 -31 23:00:00. I just want to sum all the same month values of different years. In order to do this, code at the below has been applied to my "combined" dataframe;

bymonth=aggregate(cbind(t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16,t17,t18,t19,t20,t21,t22)~month(date),data=combined,FUN=sum)

This resulted the sum all the values for same months.

> str(bymonth)
'data.frame':   12 obs. of  23 variables:
 $ month(date): num  1 2 3 4 5 6 7 8 9 10 ...
 $ t1         : num  9079539 8260669 10758842 9457569 9082702 ...
 $ t2         : num  8865401 8725852 10463748 9015349 9139077 ...
 $ t3         : num  8944534 8678897 10456126 8963046 8934908 ...
 $ t4         : num  9031512 8627635 10952759 9509983 9691009 ...
 $ t5         : num  9056095 8667163 10896578 9425384 9630523 ...
 $ t6         : num  7449536 6982570 8920602 6950115 5987614 ...
 $ t7         : num  7610326 7187122 9266084 7310302 6412976 ...
 $ t8         : num  7987144 7548964 9684426 7710839 6874969 ...
 $ t9         : num  7644122 6585329 8470816 6304882 5317381 ...
 $ t10        : num  7917904 6765170 8638459 6387506 5441643 ...
 $ t11        : num  8237490 7041719 8923193 6646191 5780772 ...
 $ t12        : num  8976847 8223188 10536015 9210887 8794828 ...
 $ t13        : num  8641208 7507814 9533105 7265526 6487144 ...
 $ t14        : num  8663482 7585256 9612939 7396403 6638494 ...
 $ t15        : num  8544334 7530451 9360095 7138325 6268128 ...
 $ t16        : num  8403307 7463580 9858757 8529255 8021458 ...
 $ t17        : num  7954798 7031909 9373369 8100460 7541611 ...
 $ t18        : num  7867333 6943056 9263699 8048171 7446287 ...
 $ t19        : num  8104703 7384206 9630303 8274850 7682145 ...
 $ t20        : num  8593316 7738650 9966859 8511077 7955871 ...
 $ t21        : num  8881166 8678495 10571128 9133670 9158251 ...
 $ t22        : num  8828751 8733658 10495228 9058206 9214007 ...

But; i just want to group sum of every month of every year. For example; all the months sum for 2005, 2006, 2007, ...

After summing all the same months for different years, i want to take average of all these months for different year. Hence; i finally want to get monthly mean results.

Thanks in advance!

1

1 Answers

0
votes

I have found the solution. Using .~month+yearis the solution. Whole command can be found at the below;

months=aggregate(cbind(t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16,t17,t18,t19,t20,t21,t22)~year(date)+month(date), FUN=sum, na.rm=TRUE, data=combined)

Do you know any more efficient way instead of writing all variables one by one as at the above?