How to do monthly average in a Table?
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Peter Perkins
2015-8-5
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
更多回答(1 个)
Brendan Hamm
2015-8-3
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 个评论
Walter Roberson
2015-8-6
I misread the release notes about when grpstats was introduced; when I look again I can no longer say when it was introduced.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!