Add, Delete, and Rearrange Table Variables
This example shows how to add, delete, and rearrange column-oriented variables in a table. You can add, move, and delete table variables using the addvars
, movevars
, and removevars
functions. As alternatives, you also can modify table variables using dot syntax or by indexing into the table. Use the splitvars
and mergevars
functions to split multicolumn variables and combine multiple variables into one. Finally, you can reorient a table so that the rows of the table become variables of an output table, using the rows2vars
function.
You also can modify table variables using the Variables Editor.
Load Sample Data and Create Tables
Load arrays of sample data from the patients
MAT-file. Display the names and sizes of the variables loaded into the workspace.
load patients whos -file patients
Name Size Bytes Class Attributes Age 100x1 800 double Diastolic 100x1 800 double Gender 100x1 13012 cell Height 100x1 800 double LastName 100x1 13216 cell Location 100x1 15808 cell SelfAssessedHealthStatus 100x1 13140 cell Smoker 100x1 100 logical Systolic 100x1 800 double Weight 100x1 800 double
Create two tables. Create one table, T
, with information collected from a patient questionnaire and create another table, T2
, with data measured from patients. Each table has 100 rows.
T = table(Age,SelfAssessedHealthStatus,Smoker); T.SelfAssessedHealthStatus = string(T.SelfAssessedHealthStatus); T2 = table(Height,Weight,Systolic,Diastolic);
Display the first five rows of each table.
head(T,5)
Age SelfAssessedHealthStatus Smoker ___ ________________________ ______ 38 "Excellent" true 43 "Fair" false 38 "Good" false 40 "Fair" false 49 "Good" false
head(T2,5)
Height Weight Systolic Diastolic ______ ______ ________ _________ 71 176 124 93 69 163 109 77 64 131 125 83 67 133 117 75 64 119 122 80
Add Variables Concatenated from Another Table
Add variables to the table T
by horizontally concatenating it with T2
.
T = [T T2];
Display the first five rows of T
.
head(T,5)
Age SelfAssessedHealthStatus Smoker Height Weight Systolic Diastolic ___ ________________________ ______ ______ ______ ________ _________ 38 "Excellent" true 71 176 124 93 43 "Fair" false 69 163 109 77 38 "Good" false 64 131 125 83 40 "Fair" false 67 133 117 75 49 "Good" false 64 119 122 80
The table T
now has 7 variables and 100 rows.
If the tables that you are horizontally concatenating have row names, horzcat
concatenates the tables by matching the row names. Therefore, the tables must use the same row names, but the row order does not matter.
Add Variable from Workspace to Table
Add the names of patients from the workspace variable LastName
before the first table variable in T
. You can specify any location in the table using the name of a variable near the new location. Use quotation marks to refer to the names of table variables. However, do not use quotation marks for input arguments that are workspace variables.
T = addvars(T,LastName,'Before',"Age"); T.LastName = string(T.LastName); head(T,5)
LastName Age SelfAssessedHealthStatus Smoker Height Weight Systolic Diastolic __________ ___ ________________________ ______ ______ ______ ________ _________ "Smith" 38 "Excellent" true 71 176 124 93 "Johnson" 43 "Fair" false 69 163 109 77 "Williams" 38 "Good" false 64 131 125 83 "Jones" 40 "Fair" false 67 133 117 75 "Brown" 49 "Good" false 64 119 122 80
You also can specify locations in a table using numbers. For example, the equivalent syntax using a number to specify location is T = addvars(T,LastName,'Before',1)
.
Add Variables Using Dot Syntax
An alternative way to add new table variables is to use dot syntax. When you use dot syntax, you always add the new variable as the last table variable. You can add a variable that has any data type, as long as it has the same number of rows as the table.
Create a new variable for blood pressure as a horizontal concatenation of the two variables Systolic
and Diastolic
. Add it to T
.
T.BloodPressure = [Systolic Diastolic]; head(T,5)
LastName Age SelfAssessedHealthStatus Smoker Height Weight Systolic Diastolic BloodPressure __________ ___ ________________________ ______ ______ ______ ________ _________ _____________ "Smith" 38 "Excellent" true 71 176 124 93 124 93 "Johnson" 43 "Fair" false 69 163 109 77 109 77 "Williams" 38 "Good" false 64 131 125 83 125 83 "Jones" 40 "Fair" false 67 133 117 75 117 75 "Brown" 49 "Good" false 64 119 122 80 122 80
T
now has 9 variables and 100 rows. A table variable can have multiple columns. So although BloodPressure
has two columns, it is one table variable.
Add a new variable, BMI
, in the table T
, that contains the body mass index for each patient. BMI
is a function of height and weight. When you calculate BMI
, you can refer to the Weight
and Height
variables that are in T
.
T.BMI = (T.Weight*0.453592)./(T.Height*0.0254).^2;
The operators ./
and .^
in the calculation of BMI
indicate element-wise division and exponentiation, respectively.
Display the first five rows of the table T
.
head(T,5)
LastName Age SelfAssessedHealthStatus Smoker Height Weight Systolic Diastolic BloodPressure BMI __________ ___ ________________________ ______ ______ ______ ________ _________ _____________ ______ "Smith" 38 "Excellent" true 71 176 124 93 124 93 24.547 "Johnson" 43 "Fair" false 69 163 109 77 109 77 24.071 "Williams" 38 "Good" false 64 131 125 83 125 83 22.486 "Jones" 40 "Fair" false 67 133 117 75 117 75 20.831 "Brown" 49 "Good" false 64 119 122 80 122 80 20.426
Move Variable in Table
Move the table variable BMI
using the movevars
function, so that it is after the variable Weight
. When you specify table variables by name, use quotation marks.
T = movevars(T,"BMI",'After',"Weight"); head(T,5)
LastName Age SelfAssessedHealthStatus Smoker Height Weight BMI Systolic Diastolic BloodPressure __________ ___ ________________________ ______ ______ ______ ______ ________ _________ _____________ "Smith" 38 "Excellent" true 71 176 24.547 124 93 124 93 "Johnson" 43 "Fair" false 69 163 24.071 109 77 109 77 "Williams" 38 "Good" false 64 131 22.486 125 83 125 83 "Jones" 40 "Fair" false 67 133 20.831 117 75 117 75 "Brown" 49 "Good" false 64 119 20.426 122 80 122 80
You also can specify locations in a table using numbers. For example, the equivalent syntax using a number to specify location is T = movevars(T,"BMI",'After',6)
. It is often more convenient to refer to variables by name.
Move Table Variable Using Indexing
As an alternative, you can move table variables by indexing. You can index into a table using the same syntax you use for indexing into a matrix.
Move BloodPressure
so that it is next to BMI
.
T = T(:,[1:7 10 8 9]); head(T,5)
LastName Age SelfAssessedHealthStatus Smoker Height Weight BMI BloodPressure Systolic Diastolic __________ ___ ________________________ ______ ______ ______ ______ _____________ ________ _________ "Smith" 38 "Excellent" true 71 176 24.547 124 93 124 93 "Johnson" 43 "Fair" false 69 163 24.071 109 77 109 77 "Williams" 38 "Good" false 64 131 22.486 125 83 125 83 "Jones" 40 "Fair" false 67 133 20.831 117 75 117 75 "Brown" 49 "Good" false 64 119 20.426 122 80 122 80
In a table with many variables, it is often more convenient to use the movevars
function.
Delete Variables
To delete table variables, use the removevars
function. Delete the Systolic
and Diastolic
table variables.
T = removevars(T,["Systolic","Diastolic"]); head(T,5)
LastName Age SelfAssessedHealthStatus Smoker Height Weight BMI BloodPressure __________ ___ ________________________ ______ ______ ______ ______ _____________ "Smith" 38 "Excellent" true 71 176 24.547 124 93 "Johnson" 43 "Fair" false 69 163 24.071 109 77 "Williams" 38 "Good" false 64 131 22.486 125 83 "Jones" 40 "Fair" false 67 133 20.831 117 75 "Brown" 49 "Good" false 64 119 20.426 122 80
Delete Variable Using Dot Syntax
As an alternative, you can delete variables using dot syntax and the empty matrix, []
. Remove the Age
variable from the table.
T.Age = []; head(T,5)
LastName SelfAssessedHealthStatus Smoker Height Weight BMI BloodPressure __________ ________________________ ______ ______ ______ ______ _____________ "Smith" "Excellent" true 71 176 24.547 124 93 "Johnson" "Fair" false 69 163 24.071 109 77 "Williams" "Good" false 64 131 22.486 125 83 "Jones" "Fair" false 67 133 20.831 117 75 "Brown" "Good" false 64 119 20.426 122 80
Delete Variable Using Indexing
You also can delete variables using indexing and the empty matrix, []
. Remove the SelfAssessedHealthStatus
variable from the table.
T(:,"SelfAssessedHealthStatus") = [];
head(T,5)
LastName Smoker Height Weight BMI BloodPressure __________ ______ ______ ______ ______ _____________ "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
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)
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)
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 in the output table, using the rows2vars
function. However, if the table has multicolumn variables, then you must split them before you can call rows2vars
.
Reorient the rows of T
. Specify that the names of the patients in T
are the names of table variables in the output table. The first variable of T3
contains the names of the variables of T
. Each remaining variable of T3
contains the data from the corresponding row of T
.
T = splitvars(T,"BP",'NewVariableNames',["Systolic","Diastolic"]); T3 = rows2vars(T,'VariableNamesSource',"LastName"); T3(:,1:5)
ans=6×5 table
OriginalVariableNames Smith Johnson Williams Jones
_____________________ ______ _______ ________ ______
{'Smoker' } 1 0 0 0
{'Height' } 71 69 64 67
{'Weight' } 176 163 131 133
{'BMI' } 24.547 24.071 22.486 20.831
{'Systolic' } 124 109 125 117
{'Diastolic'} 93 77 83 75
You can use dot syntax with T3
to access patient data as an array. However, if the row values of an input table cannot be concatenated, then the variables of the output table are cell arrays.
T3.Smith
ans = 6×1
1.0000
71.0000
176.0000
24.5467
124.0000
93.0000
See Also
table
| addvars
| movevars
| removevars
| splitvars
| mergevars
| inner2outer
| rows2vars