重命名和描述表变量
表以列向变量保留数据,并提供属性以存储有关数据的更多描述性信息。例如,变量名称存储在属性中,因此如果您要将变量重命名为更具描述性的名称,可以更改表属性来实现。此示例说明如何访问和更改表属性,包括表变量的名称、描述和单位。该示例还说明如何生成表摘要以查看这些属性以及有关表变量中数据的统计量。
从样本数据创建表
使用文件 patients.mat
中的样本患者数据的一部分创建一个表。
load patients.mat
BloodPressure = [Systolic Diastolic];
LastName = string(LastName);
T = table(LastName,Age,Height,Weight,Smoker,BloodPressure)
T=100×6 table
LastName Age Height Weight Smoker BloodPressure
__________ ___ ______ ______ ______ _____________
"Smith" 38 71 176 true 124 93
"Johnson" 43 69 163 false 109 77
"Williams" 38 64 131 false 125 83
"Jones" 40 67 133 false 117 75
"Brown" 49 64 119 false 122 80
"Davis" 46 68 142 false 121 70
"Miller" 33 64 142 true 130 88
"Wilson" 40 68 180 false 115 82
"Moore" 28 68 183 false 115 78
"Taylor" 31 66 132 false 118 86
"Anderson" 45 68 128 false 114 77
"Thomas" 42 66 137 false 115 68
"Jackson" 25 71 174 false 127 74
"White" 39 72 202 true 130 95
"Harris" 36 65 129 false 114 79
"Martin" 48 71 181 true 130 92
⋮
访问表属性
表具有可用于描述整个表及其各个变量的属性。
表将其属性存储在 Properties
对象中。要访问表的属性,请使用圆点表示法。
T.Properties
ans = TableProperties with properties: Description: '' UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {'LastName' 'Age' 'Height' 'Weight' 'Smoker' 'BloodPressure'} VariableTypes: ["string" "double" "double" "double" "logical" "double"] VariableDescriptions: {} VariableUnits: {} VariableContinuity: [] RowNames: {} CustomProperties: No custom properties are set. Use addprop and rmprop to modify CustomProperties.
您还可以使用圆点表示法访问特定属性。例如,访问存储变量名称数组的属性。
T.Properties.VariableNames
ans = 1×6 cell
{'LastName'} {'Age'} {'Height'} {'Weight'} {'Smoker'} {'BloodPressure'}
重命名表变量
变量名称在具有描述性时最有用。因此,您可能希望重命名表中的变量。
推荐的重命名变量的方法是使用 renamevars
函数。例如,将 T
的 LastName
变量重命名为 PatientName
。
T = renamevars(T,"LastName","PatientName")
T=100×6 table
PatientName Age Height Weight Smoker BloodPressure
___________ ___ ______ ______ ______ _____________
"Smith" 38 71 176 true 124 93
"Johnson" 43 69 163 false 109 77
"Williams" 38 64 131 false 125 83
"Jones" 40 67 133 false 117 75
"Brown" 49 64 119 false 122 80
"Davis" 46 68 142 false 121 70
"Miller" 33 64 142 true 130 88
"Wilson" 40 68 180 false 115 82
"Moore" 28 68 183 false 115 78
"Taylor" 31 66 132 false 118 86
"Anderson" 45 68 128 false 114 77
"Thomas" 42 66 137 false 115 68
"Jackson" 25 71 174 false 127 74
"White" 39 72 202 true 130 95
"Harris" 36 65 129 false 114 79
"Martin" 48 71 181 true 130 92
⋮
另一种重命名变量的方法是访问 T.Properties.VariableNames
属性。例如,重命名 BloodPressure
变量。
T.Properties.VariableNames("BloodPressure") = "BP"
T=100×6 table
PatientName Age Height Weight Smoker BP
___________ ___ ______ ______ ______ __________
"Smith" 38 71 176 true 124 93
"Johnson" 43 69 163 false 109 77
"Williams" 38 64 131 false 125 83
"Jones" 40 67 133 false 117 75
"Brown" 49 64 119 false 122 80
"Davis" 46 68 142 false 121 70
"Miller" 33 64 142 true 130 88
"Wilson" 40 68 180 false 115 82
"Moore" 28 68 183 false 115 78
"Taylor" 31 66 132 false 118 86
"Anderson" 45 68 128 false 114 77
"Thomas" 42 66 137 false 115 68
"Jackson" 25 71 174 false 127 74
"White" 39 72 202 true 130 95
"Harris" 36 65 129 false 114 79
"Martin" 48 71 181 true 130 92
⋮
更改其他属性
要更改任何其他表属性,必须使用圆点表示法。通常,您可以使用其他属性为表添加描述信息或变量信息作为注解。
例如,为整个表添加描述。将一个字符串赋给 Description
属性。此外,添加与表变量关联的单位。将单位的字符串数组赋给 VariableUnits
属性。虽然该属性存储一个字符向量元胞数组,但您可以使用字符串数组为其赋值。字符串数组中的单个空字符串表示对应的变量没有单位。
T.Properties.Description = "Table of Data for 100 Patients"; T.Properties.VariableUnits = ["","yr","in","lbs","","mm Hg"]; T.Properties
ans = TableProperties with properties: Description: 'Table of Data for 100 Patients' UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {'PatientName' 'Age' 'Height' 'Weight' 'Smoker' 'BP'} VariableTypes: ["string" "double" "double" "double" "logical" "double"] VariableDescriptions: {} VariableUnits: {'' 'yr' 'in' 'lbs' '' 'mm Hg'} VariableContinuity: [] RowNames: {} CustomProperties: No custom properties are set. Use addprop and rmprop to modify CustomProperties.
您还可以通过对属性进行索引来赋值。例如,仅为 PatientName
和 BP
变量添加描述。您可以按名称或按变量在表中的位置进行索引。
T.Properties.VariableDescriptions(1) = "Patient last name"; T.Properties.VariableDescriptions("BP") = "Systolic/Diastolic"; T.Properties
ans = TableProperties with properties: Description: 'Table of Data for 100 Patients' UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {'PatientName' 'Age' 'Height' 'Weight' 'Smoker' 'BP'} VariableTypes: ["string" "double" "double" "double" "logical" "double"] VariableDescriptions: {'Patient last name' '' '' '' '' 'Systolic/Diastolic'} VariableUnits: {'' 'yr' 'in' 'lbs' '' 'mm Hg'} VariableContinuity: [] RowNames: {} CustomProperties: No custom properties are set. Use addprop and rmprop to modify CustomProperties.
删除属性值
您无法删除表属性。但是,您可以删除存储在表属性中的值。
删除 LastName
变量的描述。描述是文本,因此通过赋予空字符串作为新描述来删除它。
T.Properties.VariableDescriptions(1) = "";
T.Properties
ans = TableProperties with properties: Description: 'Table of Data for 100 Patients' UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {'PatientName' 'Age' 'Height' 'Weight' 'Smoker' 'BP'} VariableTypes: ["string" "double" "double" "double" "logical" "double"] VariableDescriptions: {'' '' '' '' '' 'Systolic/Diastolic'} VariableUnits: {'' 'yr' 'in' 'lbs' '' 'mm Hg'} VariableContinuity: [] RowNames: {} CustomProperties: No custom properties are set. Use addprop and rmprop to modify CustomProperties.
删除 VariableDescriptions
中的所有描述。要删除存储在一个表属性中的所有值,请赋予一个空数组。
如果属性将文本存储在一个元胞数组中,请赋予
{}
。如果属性将数值或其他类型的值存储在一个数组中,请赋予
[]
。
T.Properties.VariableDescriptions = {}; T.Properties
ans = TableProperties with properties: Description: 'Table of Data for 100 Patients' UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {'PatientName' 'Age' 'Height' 'Weight' 'Smoker' 'BP'} VariableTypes: ["string" "double" "double" "double" "logical" "double"] VariableDescriptions: {} VariableUnits: {'' 'yr' 'in' 'lbs' '' 'mm Hg'} VariableContinuity: [] RowNames: {} CustomProperties: No custom properties are set. Use addprop and rmprop to modify CustomProperties.
对于下一节,将变量描述添加回 T
。
T.Properties.VariableDescriptions = ["Patient name","","","","True if patient smokes","Systolic and diastolic readings"]; T.Properties
ans = TableProperties with properties: Description: 'Table of Data for 100 Patients' UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {'PatientName' 'Age' 'Height' 'Weight' 'Smoker' 'BP'} VariableTypes: ["string" "double" "double" "double" "logical" "double"] VariableDescriptions: {'Patient name' '' '' '' 'True if patient smokes' 'Systolic and diastolic readings'} VariableUnits: {'' 'yr' 'in' 'lbs' '' 'mm Hg'} VariableContinuity: [] RowNames: {} CustomProperties: No custom properties are set. Use addprop and rmprop to modify CustomProperties.
对表变量数据和属性生成摘要
您可以生成表摘要以查看其属性以及有关每个变量的统计量。要生成此摘要,请使用 summary
函数。摘要显示表的描述以及每个变量的描述和单位。摘要还显示其数据类型支持所需计算的表变量的统计量。
summary(T)
T: 100×6 table Description: Table of Data for 100 Patients Variables: PatientName: string (Patient name) Age: double (yr) Height: double (in) Weight: double (lbs) Smoker: logical (34 true, True if patient smokes) BP: 2-column double (mm Hg, Systolic and diastolic readings) Statistics for applicable variables: NumMissing Min Median Max Mean Std PatientName 0 Age 0 25 39 50 38.2800 7.2154 Height 0 60 67 72 67.0700 2.8365 Weight 0 111 142.5000 202 154 26.5714 BP(:,1) 0 109 122 138 122.7800 6.7128 BP(:,2) 0 68 81.5000 99 82.9600 6.9325
您还可以将摘要存储在结构体中而不是显示它。
S = summary(T)
S = struct with fields:
PatientName: [1×1 struct]
Age: [1×1 struct]
Height: [1×1 struct]
Weight: [1×1 struct]
Smoker: [1×1 struct]
BP: [1×1 struct]
S
的每个字段都包含 T
的一个变量的描述。
S.BP
ans = struct with fields:
Size: [100 2]
Type: 'double'
Description: 'Systolic and diastolic readings'
Units: 'mm Hg'
Continuity: []
NumMissing: [0 0]
Min: [109 68]
Median: [122 81.5000]
Max: [138 99]
Mean: [122.7800 82.9600]
Std: [6.7128 6.9325]
另请参阅
table
| renamevars
| summary