0
$\begingroup$

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?

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

2 Answers 2

4
$\begingroup$

There are built-in functions to do that.

Mean/@Partition[lst,365*8]
Variance/@Partition[lst,365*8]
$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$ Apr 12, 2014 at 7:35
  • 2
    $\begingroup$ Mean/@Partition[lst,8] you can change the parameters as you want $\endgroup$
    – MMM
    Apr 12, 2014 at 7:39
1
$\begingroup$

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

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

enter image description here

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: enter image description here

You can also use TemporalData:

td = TemporalData[samp];
td2 = TemporalData`Aggregate[td, 8, Mean];

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

enter image description here

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 TemporalData`Aggregate can accept "Day"
  • I have assumed the data is complete

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.