Main Content

table 変数の名前変更と説明

列方向変数でデータを保持する table では、データに関するよりわかりやすい情報を格納できるプロパティが用意されています。たとえば、変数名はプロパティに格納されるため、変数の名前をよりわかりやすい名前に変更する場合は、table のプロパティを変更して名前を変更できます。この例では、table 変数の名前、説明、単位など、table のプロパティにアクセスおよび変更する方法を示します。この例では、table 変数内のデータに関する統計とともにこれらのプロパティを表示する table の概要を生成する方法も示します。

サンプル データからの table の作成

ファイル patients.mat からサンプル患者データのサブセットを使用して table を作成します。

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  
      ⋮

テーブル プロパティへのアクセス

table には、table を全体として説明したり、その個々の変数を説明したりするために使用できるプロパティがあります。

table はそのプロパティを Properties オブジェクトに保存します。table のプロパティにアクセスするには、ドット表記を使用します。

T.Properties
ans = 
  TableProperties with properties:

             Description: ''
                UserData: []
          DimensionNames: {'Row'  'Variables'}
           VariableNames: {'LastName'  'Age'  'Height'  'Weight'  'Smoker'  'BloodPressure'}
    VariableDescriptions: {}
           VariableUnits: {}
      VariableContinuity: []
                RowNames: {}
        CustomProperties: No custom properties are set.
      Use addprop and rmprop to modify CustomProperties.

ドット表記を使用して特定のプロパティにアクセスすることもできます。たとえば、変数名の配列を格納するプロパティにアクセスします。

T.Properties.VariableNames
ans = 1x6 cell
    {'LastName'}    {'Age'}    {'Height'}    {'Weight'}    {'Smoker'}    {'BloodPressure'}

table 変数の名前変更

変数名はわかりやすいものにすると最も役立ちます。そのため、table で変数の名前を変更できます。

変数の名前を変更するには、関数 renamevars を使用する方法が推奨されます。たとえば、T の変数 LastNamePatientName に名前変更します。

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
      ⋮

その他のプロパティの変更

他の table のプロパティを変更するには、ドット表記を使用する必要があります。一般的に、その他のプロパティを使用して、table や変数について説明する情報を使用して table に注釈を付けることができます。

たとえば、table 全体としての説明を追加します。Description プロパティに string を代入します。また、table 変数に関連付けられた単位を追加します。単位の string 配列を VariableUnits プロパティに代入します。このプロパティは文字ベクトルの cell 配列を格納しますが、string 配列を使用してそれに値を代入できます。string 配列内の個々の空の string は、対応する変数に単位がないことを示します。

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'}
    VariableDescriptions: {}
           VariableUnits: {''  'yr'  'in'  'lbs'  ''  'mm Hg'}
      VariableContinuity: []
                RowNames: {}
        CustomProperties: No custom properties are set.
      Use addprop and rmprop to modify CustomProperties.

プロパティのインデックス付けを行って値を代入することもできます。たとえば、変数 PatientName および BP のみの説明を追加します。名前で、または table 内の変数の位置でインデックスを付けることができます。

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'}
    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.

プロパティの値の削除

table のプロパティを削除することはできません。ただし、table のプロパティに格納されている値を削除することはできます。

変数 LastName の説明を削除します。説明はテキストであるため、新しい説明として空 string を代入することで説明を削除します。

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'}
    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 内のすべての説明を削除します。table のプロパティに保存されているすべての値を削除するには、空の配列を代入します。

  • プロパティで cell 配列にテキストが保存されている場合は、{} を代入します。

  • プロパティで配列に数値または他の型の値が保存されている場合は、[] を代入します。

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'}
    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'}
    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.

table 変数データおよびプロパティの概要の生成

table の概要を生成して、各変数に関する統計とともにそのプロパティを表示できます。この概要を生成するには、関数 summary を使用します。概要には、table の説明および各変数の説明と単位が表示されます。概要では、必要な計算をサポートするデータ型をもつ table 変数の統計も表示されます。

summary(T)
Description:  Table of Data for 100 Patients

Variables:

    PatientName: 100x1 string

        Properties:
            Description:  Patient name
    Age: 100x1 double

        Properties:
            Units:  yr
        Values:

            Min          25   
            Median       39   
            Max          50   

    Height: 100x1 double

        Properties:
            Units:  in
        Values:

            Min          60   
            Median       67   
            Max          72   

    Weight: 100x1 double

        Properties:
            Units:  lbs
        Values:

            Min          111  
            Median     142.5  
            Max          202  

    Smoker: 100x1 logical

        Properties:
            Description:  True if patient smokes
        Values:

            True        34   
            False       66   

    BP: 100x2 double

        Properties:
            Units:  mm Hg
            Description:  Systolic and diastolic readings
        Values:
                      Column 1    Column 2
                      ________    ________

            Min         109           68  
            Median      122         81.5  
            Max         138           99  

概要を表示するのではなく、構造体に保存することもできます。

S = summary(T)
S = struct with fields:
    PatientName: [1x1 struct]
            Age: [1x1 struct]
         Height: [1x1 struct]
         Weight: [1x1 struct]
         Smoker: [1x1 struct]
             BP: [1x1 struct]

S の各フィールドには、T の変数の説明が含まれます。

S.BP
ans = struct with fields:
           Size: [100 2]
           Type: 'double'
    Description: 'Systolic and diastolic readings'
          Units: 'mm Hg'
     Continuity: []
            Min: [109 68]
         Median: [122 81.5000]
            Max: [138 99]
     NumMissing: [0 0]

参考

| |

関連するトピック