# How to partition three years of data into daily samples

I'm trying to find mean of temperature data list. The temperature data list has 3 hours steps which means I have 365*8=2920 temperature measurements per year. My data are temperatures of 13 years. First I found mean temperature of 13 years "t" one by one list by the following:

 MeanTemp={Mean[Transpose[Partition[t[[1 ;; 365*8]], 8], {2, 1}]],
Mean[Transpose[Partition[t[[365*8 + 1 ;; 365*8*2]], 8], {2, 1}]],
Mean[Transpose[Partition[t[[365*8*2 + 1 ;; 365*8*3]], 8], {2, 1}]],
Mean[Transpose[Partition[t[[365*8*3 + 1 ;; 365*8*4]], 8], {2, 1}]],
Mean[Transpose[Partition[t[[365*8*4 + 1 ;; 365*8*5]], 8], {2, 1}]],
Mean[Transpose[Partition[t[[365*8*5 + 1 ;; 365*8*6]], 8], {2, 1}]],
Mean[Transpose[Partition[t[[365*8*6 + 1 ;; 365*8*7]], 8], {2, 1}]],
Mean[Transpose[Partition[t[[365*8*7 + 1 ;; 365*8*8]], 8], {2, 1}]],
Mean[Transpose[Partition[t[[365*8*8 + 1 ;; 365*8*9]], 8], {2, 1}]],
Mean[Transpose[Partition[t[[365*8*9 + 1 ;; 365*8*10]], 8], {2, 1}]],
Mean[Transpose[Partition[t[[365*8*10 + 1 ;; 365*8*11]], 8], {2, 1}]],
Mean[Transpose[
Partition[n[[365*8*11 + 1 ;; 365*8*12]], 8], {2, 1}]],
Mean[Transpose[Partition[n[[365*8*12 + 1 ;; 365*8*13]], 8], {2, 1}]]};


Then I subtract mean temperature of all years from it. Which means I found variance of temperature:

TempVariance={Mean[Transpose[Partition[t[[1 ;; 365*8]], 8], {2, 1}]] -
meanofyear,
Mean[Transpose[Partition[t[[365*8 + 1 ;; 365*8*2]], 8], {2, 1}]] -
meanofyear,
Mean[Transpose[Partition[t[[365*8*2 + 1 ;; 365*8*3]], 8], {2, 1}]] -
meanofyear,
Mean[Transpose[Partition[t[[365*8*3 + 1 ;; 365*8*4]], 8], {2, 1}]] -
meanofyear,
Mean[Transpose[Partition[t[[365*8*4 + 1 ;; 365*8*5]], 8], {2, 1}]] -
meanofyear,
Mean[Transpose[Partition[t[[365*8*5 + 1 ;; 365*8*6]], 8], {2, 1}]] -
meanofyear,
Mean[Transpose[Partition[t[[365*8*6 + 1 ;; 365*8*7]], 8], {2, 1}]] -
meanofyear,
Mean[Transpose[Partition[t[[365*8*7 + 1 ;; 365*8*8]], 8], {2, 1}]] -
meanofyear,
Mean[Transpose[Partition[t[[365*8*8 + 1 ;; 365*8*9]], 8], {2, 1}]] -
meanofyear,
Mean[Transpose[Partition[t[[365*8*9 + 1 ;; 365*8*10]], 8], {2, 1}]] -
meanofyear,
Mean[Transpose[
Partition[t[[365*8*10 + 1 ;; 365*8*11]], 8], {2, 1}]] -
meanofyear,
Mean[Transpose[
Partition[t[[365*8*11 + 1 ;; 365*8*12]], 8], {2, 1}]] -
meanofyear,
Mean[Transpose[
Partition[t[[365*8*12 + 1 ;; 365*8*13]], 8], {2, 1}]] -
meanofyear}


I think this manuscript would be so long if the data increases.How to decrease the manuscript and increase flexibility?

• Have a deeper look at Partition...among other things.
– ciao
Apr 12, 2014 at 7:13
• Please, can you paste a chunk of your data set here ? May be the task you have asked about can be accomplished rapidly .... Apr 12, 2014 at 18:31

There are built-in functions to do that.

Mean/@Partition[lst,365*8]
Variance/@Partition[lst,365*8]

• Mean/@Partition[lst,365*8] gives me only 13 values of 13 years. I want 13*365=4745 values. Mean[Transpose[Partition[t[[1 ;; 365*8]], 8], {2, 1}]] gives me first year's average 365 values. Apr 12, 2014 at 7:35
• Mean/@Partition[lst,8] you can change the parameters as you want
– MMM
Apr 12, 2014 at 7:39

I present this for illustrative purposes. Here is a toy data set:

samp = RandomReal[{23, 32}, 365 8 ];


This is just 365 days of 8 samples per day. You can get daily mean using:

Mean /@ Partition[samp, 8];


You can visualize by just wrapping in ListPlot and with option Joined->True:

You can also use TemporalData:

td = TemporalData[samp];
td2 = TemporalDataAggregate[td, 8, Mean];


The second line bins the temporal data into bins of width 8 and applies Mean and yields same result.

Note:

• in a 13 year period there will be leap years, hence total number of observations not a multiple of 365
• if your data has datetime stamps TemporalDataAggregate can accept "Day"
• I have assumed the data is complete

You can adapt to desired intervals of interest: month, quarter, year, etc.