Split and Merge Table Variables
To split multicolumn table variables into variables that each have one column, use the
splitvars functions. Split the variable BloodPressure into two variables.
T = splitvars(T,'BloodPressure','NewVariableNames',{'Systolic','Diastolic'});
head(T,5)
ans=5×7 table
LastName Smoker Height Weight BMI Systolic Diastolic
__________ ______ ______ ______ ______ ________ _________
'Smith' true 71 176 24.547 124 93
'Johnson' false 69 163 24.071 109 77
'Williams' false 64 131 22.486 125 83
'Jones' false 67 133 20.831 117 75
'Brown' false 64 119 20.426 122 80
Similarly, you can group related table variables together in one variable, using the
mergevars function. Combine Systolic and Diastolic back into one variable, and
name it BP.
T = mergevars(T,{'Systolic','Diastolic'},'NewVariableName','BP');
head(T,5)
ans=5×6 table
LastName Smoker Height Weight BMI BP
__________ ______ ______ ______ ______ __________
'Smith' true 71 176 24.547 124 93
'Johnson' false 69 163 24.071 109 77
'Williams' false 64 131 22.486 125 83
'Jones' false 67 133 20.831 117 75
'Brown' false 64 119 20.426 122 80
Reorient Rows To Become Variables
You can reorient the rows of a table or timetable, so that they become the variables the
output table, using the rows2vars function. However, if the table has multicolumn
variables, then you must split them before you can call rows2vars.
9 Tables