Can you have a multilevel table?
Show older comments
I have a large table that has several groups of similar variables. I'd like to create levels of variables so I can group and access them easily- perhaps I need a struct or perhaps I am being lazy.
load patients
T1 = table(LastName,Gender,Age,Height,Weight,Systolic,Diastolic);
head(T1,3)
I can merge variables, but I seem to lose the nested variable names.
T2 = mergevars(T1,{'Systolic','Diastolic'},'NewVariableName','BloodPressure');
head(T2,3)
I'd like to be able to access the Systolic and Diastolic variable under BloodPressure like this:
T2.BloodPressure.Systolic
%but not this
T2.BloodPressure(:,1)
It's a lot harder for me to keep track of the index, and I am hoping I would be able to use Tab Completion.
Also, in the event there is more than one 'Blood Pressure', say I have BloodPressure1 and BloodPressure2 both with sub variables Systolic and DIastolic, I'd like to be able to get all the Systolics at once- something like
T2.{:}.Systolic
Hope that explains what I'm after, and hoping there's a way to get there. Thanks.
Accepted Answer
More Answers (1)
An alternative to @Voss's execellent answer is to create the nested table(s) and then add the nested table(s) as a table variable to the main table (MATLAB R2018b or later).
load patients
BloodPressure = table(Systolic,Diastolic);
T1 = table(LastName,Gender,Age,Height,Weight, BloodPressure)
3 Comments
I really like nested tables, but how can I run groupsummary on them? Without having the grouping functions supporting nested tables I don't see any advantages..
load patients
BloodPressure = table(Systolic,Diastolic);
T1 = table(LastName,Gender,Age,Height,Weight, BloodPressure)
groupsummary(T1,"Gender",@(x)mean(x.Systolic),"BloodPressure")
> I really like nested tables, but how can I run groupsummary on them?
Demo:
t3 = table(randi(2,5,1),rand(5,1),'VariableNames',{'a','b'});
t2 = table(rand(5,1),rand(5,1),t3,t3,'VariableNames',{'c','d','t3','t4'})
t1 = splitvars(t2)
groupsummary(t1,'t3_a', 'mean', 't3_b')
Jan Kappen
on 7 Feb 2023
@Adam Danz that's a very nice information, thx.
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!