How to transform a table to a nested structure comparable to a pivot table in excel?
4 views (last 30 days)
Show older comments
I have a table which I want to structure into a nested structure to get something like a pivot table in excel, where the data is structured according to certain conditions so that I can calculate means and standard deviations for different study groups.
E.g. I want to calculate the mean of value 1 for all data where test='A', condition='C', participant='X' and date='d1'
The table I have looks like this:
% Build sample table
test=["A";"A";"B";"A";"B"];
condition=["C";"RT";"C";"C";"RT"];
participant=["X";"X";"Y";"Z";"Y"];
date=["d1";"d2";"d1";"d1";"d3"];
sample=[1;2;3;4;5];
value1=[10;11;12;10;12];
value2=[3;4;2;5;5];
value3=[13;16;25;25;36];
T=table(test,condition,participant,date,value1,value2,value3)
And the nested structure I want to get should look somewhat like that:
% Build nested structure
ssample=[1;2;3];
svalue1=[4;7;12];
svalue2=[3;9;1];
svalue3=[4;34;12];
s=table(ssample,svalue1,svalue2,svalue3);
m=struct('d1',s,'d2',s,'d3',s);
n=struct('C',m,'RT',m);
o=struct('X', n, 'Y', n, 'Z',n);
EVAL=struct('A',o,'B',o);
I don't want to assign all data seperatly since my actual table is 300x200. Is there anyway to do that at least semi-automatic?
Thanks fo any suggestions!
0 Comments
Accepted Answer
Stephen23
on 10 Dec 2022
"I want to calculate the mean of value 1 for all data where test='A', condition='C', participant='X' and date='d1'"
The MATLAB approach:
% Build sample table
test=["A";"A";"B";"A";"B"];
condition=["C";"RT";"C";"C";"RT"];
participant=["X";"X";"Y";"Z";"Y"];
date=["d1";"d2";"d1";"d1";"d3"];
sample=[1;2;3;4;5];
value1=[10;11;12;10;12];
value2=[3;4;2;5;5];
value3=[13;16;25;25;36];
T=table(test,condition,participant,date,value1,value2,value3)
G = groupsummary(T,["test","condition","participant","date"],"mean","value1")
X = G.test=="A" & G.condition=="C" & G.participant=="X" & G.date=="d1";
G{X,'mean_value1'}
2 Comments
Peter Perkins
on 12 Dec 2022
A bit more on Stephen23's answer:
You have four grouping variables, so a cross-tabulation is 4-D. However, the format of a pivot table in Excel is necessily 2-D, so instead Excel nests some of those dimensions. On the other hand, groupsummary flattens those 4 dimensions into a table with four grouping variables and however many summary vars you want. Stephen23 computed the mean of value1, but that summary table could just as easily had means of all three of your data vars. And it could have contained the mean AND std dev for all three.
I would also recommend using categorical grouping variables where possible instead of string.
More Answers (1)
Lola Davidson
on 17 Mar 2023
As of R2023a, you can use the "pivot" function to orient the data more like what excel provides: https://www.mathworks.com/help/matlab/ref/pivot.html
For example, you could put two variables along the rows, and two along the columns:
P = pivot(T, Rows=["test" "condition"], Columns=["participant","date"], DataVariable="value1", Method="mean")
This provides a nested table as the output. You can get vertical slices of the data using dot indexing like with structs:
>> P.X.d1
0 Comments
See Also
Categories
Find more on Tables 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!