Transpose pivot reshape a MATLAB table from a cross-section wide 'row format' to a panel-data long 'column' format
20 views (last 30 days)
Show older comments
I have a large table with cross-section data that I want to convert into a panel/long record, see example below i.e. 'Data structure wanted'. My question is how to get to the 'data structure wanted' from the source MATLAB table?
Note that the following MATLAB central code - which uses the cellmat and mat2cell function - works well for the problem at hand: https://nl.mathworks.com/matlabcentral/answers/73483-transposing-blocks-of-matrices-from-a-bigger-initial-matrix#answer_83415. The code is somewhat difficult to explain to new and inexperienced MATLAB users though.
What are code alternatives - without looping - to create a panel data structure for the example below? Peferably this code is flexible for a variable number of companies and variables.
clc,clear
% The data reads as: company-id (col 1), variable-id (col 2), and an 'array of values' for 3 years (col 3-5).
data=[1 1 1 2 3 ;
1 2 11 22 33 ;
2 1 111 222 333;
2 2 1111 2222 3333]; % 4*5
T = array2table(data, 'VariableNames',{'CoId','VarId','Y1', 'Y2', 'Y3'}); % no multi column
display('Source data structure:');
T
%T = 4×5 table
% CoId VarId Y1 Y2 Y3
% ____ _____ ____ ____ ____
% 1 1 1 2 3
% 1 2 11 22 33
% 2 1 111 222 333
% 2 2 1111 2222 3333
% Wanted: CoId VarId_1 VarId_2
b=[1 1 11; 1 2 22; 1 3 33; 2 111 4444; 2 222 4444; 2 333 4444];
T = array2table(b);
T.Properties.VariableNames=["CoId","VarId_1","VarId_2"]; % Add variable names
display('Data structure wanted:');
T
% T =
% 6×3 table
% CoId VarId_1 VarId_2
% ____ _______ _______
% 1 1 11
% 1 2 22
% 1 3 33
% 2 111 4444
% 2 222 4444
% 2 333 4444
0 Comments
Answers (1)
Steven Lord
on 8 Sep 2021
I'm not sure where those 4444 values came from in your second T variable, but I think what you want is to stack your table data. See the examples on the documentation page and see if they look like what you're trying to do.
If you're working in the Live Editor you could use the Stack Table Variables task to experiment with the various options interactively.
Or perhaps you're trying to do what I thought of when I first read the subject of your question which was to exchange rows and variables in the table using the rows2vars function.
See Also
Categories
Find more on Convert Image Type in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!