request advice on groupfilter in timetables

2 次查看(过去 30 天)
Would greatly appreciate some advice on what should be a simple operation with groupfilter. The script under construction below needs to filter for (isolate) all the rows & columns pertaining to one ticker at a time.
load SPMIDFund.mat;
Fbatch = {'GGG','WSM','CGNX','CRL','FICO','MOH'};
for corpind = 1:length(Fbatch);
corp = Fbatch{corpind};
%groupfilter(SPMIDFund,
date = SPMIDFund.CALENDARDATE;
repdate = SPMIDFund.LASTUPDATED;
sls = SPMIDFund.REVENUE;
ps = SPMIDFund.PS
% etc.
% etc.
% assignin
% save
end
The data source is a 191x91 timetable already binned by ticker (fragment attached)--I need to also filter for that same ticker. 'TICKER' is the only column denoting the company associated with the earnings data in the rest of the table. Have not been able to find a groupfilter or rowfun syntax that works. Suggestions will be appreciated.
CC
  8 个评论
Peter Perkins
Peter Perkins 2021-7-30
You almost certainly don't want to do that. What you probably want is to work in place, rather than creating a bunch of things in your workspace with dynamic names. It's a FAQ that maybe someone can point to.
What you more likely want to do is to use groupsummary, or one of its siblings, or maybe a grouped rowfun. The lowwing is rowfun, group* can do the same things.
function [x,y] = myfun1(a,b,c,d) % a nonsense reduction function
x = mean(a - b);
y = var(c - d);
end
>> load SPMIDFund1.mat
>> rowfun(@myfun1,SPMIDFund1,"InputVariables",["DATEKEY" "LASTUPDATED" "ASSETS" "ACCOCI"],...
"GroupingVariables","TICKER","OutputVariableNames",["X" "Y"], "OutputFormat","table")
ans =
2×4 table
TICKER GroupCount X Y
______ __________ ____________ __________
CGNX 21 -21990:51:25 1.2562e+17
CRL 5 -39360:00:00 9.1939e+16
function [x,y] = myfun2(a,b,c,d) % a nonsense transformation function
x = a - b;
y = c - mean(c)./d;
end
>> rowfun(@myfun2,SPMIDFund1,"InputVariables",["DATEKEY" "LASTUPDATED" "ASSETS" "ACCOCI"],...
"GroupingVariables","TICKER","OutputVariableNames",["X" "Y"])
ans =
26×4 timetable
CALENDARDATE TICKER GroupCount X Y
____________ ______ __________ ____________ __________
31-Mar-2016 CGNX 21 -43944:00:00 9.1364e+08
30-Jun-2016 CGNX 21 -41760:00:00 9.6444e+08
30-Sep-2016 CGNX 21 -39576:00:00 1.0201e+09
31-Dec-2016 CGNX 21 -36984:00:00 1.0386e+09
31-Mar-2017 CGNX 21 -35208:00:00 1.0962e+09
30-Jun-2017 CGNX 21 -33024:00:00 1.161e+09
30-Sep-2017 CGNX 21 -30840:00:00 1.2585e+09
31-Dec-2017 CGNX 21 -28248:00:00 1.2879e+09
31-Mar-2018 CGNX 21 -26472:00:00 1.2689e+09
30-Jun-2018 CGNX 21 -24288:00:00 1.2763e+09
30-Sep-2018 CGNX 21 -22104:00:00 1.3247e+09
31-Dec-2018 CGNX 21 -19512:00:00 1.2897e+09
31-Mar-2019 CGNX 21 -17736:00:00 1.3538e+09
30-Jun-2019 CGNX 21 -15552:00:00 1.3499e+09
30-Sep-2019 CGNX 21 -13368:00:00 1.3905e+09
31-Dec-2019 CGNX 21 -10776:00:00 1.8859e+09
31-Mar-2020 CGNX 21 -9000:00:00 1.8851e+09
30-Jun-2020 CGNX 21 -6768:00:00 1.9349e+09
30-Sep-2020 CGNX 21 -4584:00:00 2.0605e+09
31-Dec-2020 CGNX 21 -2040:00:00 1.8007e+09
31-Mar-2021 CGNX 21 -24:00:00 1.909e+09
31-Mar-2016 CRL 5 -43824:00:00 2.1037e+09
30-Jun-2016 CRL 5 -41640:00:00 2.763e+09
30-Sep-2016 CRL 5 -39456:00:00 2.6902e+09
31-Dec-2016 CRL 5 -36960:00:00 2.7118e+09
31-Mar-2017 CRL 5 -34920:00:00 2.6971e+09
If you are determined to work on one timetable at a time, write a loop over the unique values of SPMIDFund1, and grab a subtable at each iteration:
for ticker = unique(SPMIDFund1.Ticker)'
t_i = SPMIDFund1(SPMIDFund1.Ticker == ticker,:)
<do something>
end
cris
cris 2021-8-2
Will try your solution tomorrow (Mon). Unfortunately, it will be necessary to split these tables in due course in order to use in a different script.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Preprocessing 的更多信息

产品


版本

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by