필터 지우기
필터 지우기

Using splitapply to add a column to a table

조회 수: 2 (최근 30일)
Bill
Bill 2022년 5월 4일
댓글: Star Strider 2023년 8월 7일
I have data in a matlab table in long format. It contains stock tickers and monthly returns. I'd like to group the data by tickers, than grab the next periods return, and make a column of that next periods return. Here is my code. It returns cell arrays for each group. Is there a way to put each cell array back into Tbl with a new a column named NextMonthsReturn? Thanks!
Tickers = ["AAPL"; "AAPL"; "AAPL"; "MSFT"; "MSFT"; "MSFT"];
MonthlyReturns = [4.3;3.2;5.6;7.3;2.5;-5.6];
Tbl = table(Tickers, MonthlyReturns);
Grps = findgroups(Tbl.('Tickers'));
Rslts = splitapply(@(x) {[x(2:end);nan(1,1)]}, Tbl.('MonthlyReturns'), Grps);
Rslts

채택된 답변

Star Strider
Star Strider 2022년 5월 4일
I am not certain what result you want.
Try this —
Tickers = ["AAPL"; "AAPL"; "AAPL"; "MSFT"; "MSFT"; "MSFT"];
MonthlyReturns = [4.3;3.2;5.6;7.3;2.5;-5.6];
Tbl = table(Tickers, MonthlyReturns)
Tbl = 6×2 table
Tickers MonthlyReturns _______ ______________ "AAPL" 4.3 "AAPL" 3.2 "AAPL" 5.6 "MSFT" 7.3 "MSFT" 2.5 "MSFT" -5.6
Grps = findgroups(Tbl.('Tickers'));
Rslts = splitapply(@(x) {[x(2:end);nan(1,1)]}, Tbl.('MonthlyReturns'), Grps);
Tbl = addvars(Tbl,cat(1,Rslts{:}),'NewVariableNames',{'NextMonthsReturn'})
Tbl = 6×3 table
Tickers MonthlyReturns NextMonthsReturn _______ ______________ ________________ "AAPL" 4.3 3.2 "AAPL" 3.2 5.6 "AAPL" 5.6 NaN "MSFT" 7.3 2.5 "MSFT" 2.5 -5.6 "MSFT" -5.6 NaN
See the documentation for addvars and cat for details on those functions.
.
  댓글 수: 4
Simon
Simon 2023년 8월 7일
편집: Simon 2023년 8월 7일
If Tickers are not sorted, either Tbl or Rslts needs to be sorted before addvars. How to sort Tbl so the rows in the same group are put next to each other? Sorting the group can work, but will it have any bug? It seems not able to maintain the original within group order. That worries me.
Tickers_unsorted = ["AAPL"; "AAPL"; "MSFT"; "MSFT"; "MSFT"; "AAPL"];
MonthlyReturns = [4.3; 5.6; 7.3; 2.5; -5.6; 3.2];
Tbl = table(Tickers_unsorted, MonthlyReturns);
Grps = findgroups(Tbl.('Tickers2'))
% not in correct order
Rslts = splitapply(@(x) {[x(2:end);nan(1,1)]}, Tbl.('MonthlyReturns'), Grps);
% need to sort table rows so same group rows are next to each other
[~, ix] = sort(Grps);
Tbl = Tbl(ix,:);
Tbl = addvars(Tbl,cat(1,Rslts{:}),'NewVariableNames',{'NextMonthsReturn'})
Star Strider
Star Strider 2023년 8월 7일
@Simon — I’m not certain that was a problem here, however it doesn’t specifically concern the problem this post is addressing (adding the variable to the existing table), since the column to be added has already been calculated.
It would likely be better for you to post this as a new question, citing to this or copying the code. I will not address it here.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt J
Matt J 2022년 5월 4일
편집: Matt J 2022년 5월 4일
Tickers = ["AAPL"; "AAPL"; "AAPL"; "MSFT"; "MSFT"; "MSFT"];
MonthlyReturns = [4.3;3.2;5.6;7.3;2.5;-5.6];
Tbl = table(Tickers, MonthlyReturns);
out=varfun(@(x) [x(2:end);nan(1,1)],Tbl,'Input','MonthlyReturns','Group','Tickers');
Tbl.NextMonthsReturns=out{:,3}
Tbl = 6×3 table
Tickers MonthlyReturns NextMonthsReturns _______ ______________ _________________ "AAPL" 4.3 3.2 "AAPL" 3.2 5.6 "AAPL" 5.6 NaN "MSFT" 7.3 2.5 "MSFT" 2.5 -5.6 "MSFT" -5.6 NaN

카테고리

Help CenterFile Exchange에서 App Building에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by