0

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?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
user2987529
  • 145
  • 5

2 Answers2

4

There are built-in functions to do that.

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

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.

ubpdqn
  • 60,617
  • 3
  • 59
  • 148