Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
Have a deeper look at Partition...among other things. –  rasher Apr 12 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 .... –  mitochondrial Apr 12 at 18:31

2 Answers 2

There are built-in functions to do that.

Mean/@Partition[lst,365*8]
Variance/@Partition[lst,365*8]
share|improve this answer
    
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. –  user2987529 Apr 12 at 7:35
2  
Mean/@Partition[lst,8] you can change the parameters as you want –  Jason Apr 12 at 7:39

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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