How to do monthly average in a Table?
5 views (last 30 days)
Show older comments
Hi,
I have a Matlab Table that has 3 columns. The first column is dates, the 2nd column is Term (integer number), and the 3rd column is prices (double).
The columns are: Dates Terms Prices
I want to create a new table, that the the dates of only the first of each month, and have the average price for each month.
I thought it should be easy to do, as I can do it in Excel pivot table by hand.
How to do that in Matlab? I imagine Matlab should make it easier. Besides, I have many such Excel so I want to do it more efficiently.
Thanks,
Jennifer
0 Comments
Accepted Answer
Peter Perkins
on 5 Aug 2015
Even without Statistics And Machine Learning Toolbox (or Statistics Toolbox prior to R2015a), the varfun method of table also does this kind of grouped calculation. (And for the record, grpstats did exist in Statistics Toolbox well before R2014a.)
Jennifer, the example table that you provided does not match your original description. Term in the table is a date string, not an integer. I'll assume you have a table like this:
>> data
data =
Datetime Term Price
____________ ___________ ______
'12/7/2005' '12/1/2005' 106
'12/8/2005' '12/1/2005' 106
'12/22/2005' '12/1/2005' 106
'12/23/2005' '12/1/2005' 106
'12/28/2005' '12/1/2005' 106
'12/29/2005' '12/1/2005' 106
'12/30/2005' '12/1/2005' 106
'12/7/2005' '1/1/2006' 117.25
'12/8/2005' '1/1/2006' 121.48
'12/22/2005' '1/1/2006' 115.57
Grouping by month, unless your data span less than 12 months, really means, "group by month and year". I'll assume you are in the general case.
There are better ways to do this using the datetime data type, new in R2014b. But here's a way to do it with strings:
>> dv = datevec(data.Datetime);
>> data.Year = dv(:,1);
>> data.Month = dv(:,2)
data =
Datetime Term Price Year Month
____________ ___________ ______ ____ _____
'12/7/2005' '12/1/2005' 106 2005 12
'12/8/2005' '12/1/2005' 106 2005 12
'12/22/2005' '12/1/2005' 106 2005 12
'12/23/2005' '12/1/2005' 106 2005 12
'12/28/2005' '12/1/2005' 106 2005 12
'12/29/2005' '12/1/2005' 106 2005 12
'12/30/2005' '12/1/2005' 106 2005 12
'12/7/2005' '1/1/2006' 117.25 2005 12
'12/8/2005' '1/1/2006' 121.48 2005 12
'12/22/2005' '1/1/2006' 115.57 2005 12
>> monthlyAvg = varfun(@mean,data,'GroupingVariables',{'Year','Month'},'InputVariable','Price')
monthlyAvg =
Year Month GroupCount mean_Price
____ _____ __________ __________
2005_12 2005 12 10 109.63
Obviously if your example data had contained rows from more than just Dec 2005, that result would have more than one row. Not sure what you mean by, "also want to group by the term". You may want to group both by month/year, and by Term. Or you might want a separate result, grouped just by Term. Either is straight-forward, here's the latter.
>> termAvg = varfun(@mean,data,'GroupingVariables','Term','InputVariable','Price')
termAvg =
Term GroupCount mean_Price
___________ __________ __________
1/1/2006 '1/1/2006' 3 118.1
12/1/2005 '12/1/2005' 7 106
More Answers (1)
Brendan Hamm
on 3 Aug 2015
If you have the Statistics and MAchine Learning Toolbox, this can be done quite easily. For simplicity I will assume your Table T has the variables: Date, Terms and Prices. What we want to do is use the Month as a grouping variable. We can get the month from a datetime with the month function:
mth = month(T.Dates); % Numeric Values (optional input 'name' will give the full name)
Now we want to calculate the mean for each month which is easy to do with grpstats which will calculate statistics of its first input, grouped by the second input:
doc grpstats
[monthlyPriceMean,groupName] = grpstats(T.Prices,mth,{'mean','gname'});
monthTable = table(groupName,monthlyPriceMean); % Place in a table
You could do the same thing with the Terms. If you really want to apply this and get a table back out immediatelly, you could always use this in conjunction with varfun.
7 Comments
Walter Roberson
on 6 Aug 2015
I misread the release notes about when grpstats was introduced; when I look again I can no longer say when it was introduced.
See Also
Categories
Find more on Calendar in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!