How to transform a table to a nested structure comparable to a pivot table in excel?
Afficher commentaires plus anciens
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!
Réponse acceptée
Plus de réponses (1)
Lola Davidson
le 17 Mar 2023
1 vote
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
Catégories
En savoir plus sur Tables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!