Main Content

このページの翻訳は最新ではありません。ここをクリックして、英語の最新版を参照してください。

table 変数の名前変更と説明

table の列方向の変数にはデータが含まれます。ただし、table には、table とその変数に関する追加の説明情報を保存できるプロパティも用意されています。たとえば、変数名はプロパティです。もっとわかりやすい名前に変更したい場合があります。変数名を変更するには、関数 renamevars を使用します。table には、変数の説明や変数に関連付けられている単位などの他のプロパティもあります。table はそのプロパティを Properties オブジェクトに保存します。説明および単位を Properties オブジェクトに追加するには、ドット表記を使用します。table 変数に関する統計と変数の名前、説明、および単位を組み合わせた、table の概要を生成することもできます。この概要を生成するには、関数 summary を使用します。

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

ファイル patients.mat からサンプル患者データを読み込みます。このファイルには、さまざまなデータ型の配列が含まれています。

load patients.mat
whos
  Name                            Size            Bytes  Class      Attributes

  Age                           100x1               800  double               
  Diastolic                     100x1               800  double               
  Gender                        100x1             11412  cell                 
  Height                        100x1               800  double               
  LastName                      100x1             11616  cell                 
  Location                      100x1             14208  cell                 
  SelfAssessedHealthStatus      100x1             11540  cell                 
  Smoker                        100x1               100  logical              
  Systolic                      100x1               800  double               
  Weight                        100x1               800  double               

これらの配列のサブセットから table を作成します。入力配列を変更する必要がある場合は、table を作成する前に変更できます。たとえば、SystolicDiastolic を結合して 100 行 2 列の行列にします。次に、LastName を string 配列に変換します。

BloodPressure = [Systolic Diastolic];
LastName = string(LastName);

入力配列から table を作成するには、関数 table を使用します。

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 には他のプロパティもあります。一部のプロパティでは、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 変数に関連付けられている単位をリストする string 配列を追加します。それを VariableUnits プロパティに代入します。このプロパティは文字ベクトルの cell 配列ですが、string 配列を使用してそれに値を代入できます。string 配列内の個々の空の string は、対応する変数に単位がないことを示します。

T.Properties.VariableUnits = ["","Yrs","In","Lbs","","mm Hg"];
T.Properties
ans = 
  TableProperties with properties:

             Description: ''
                UserData: []
          DimensionNames: {'Row'  'Variables'}
           VariableNames: {'PatientName'  'Age'  'Height'  'Weight'  'Smoker'  'BP'}
    VariableDescriptions: {}
           VariableUnits: {''  'Yrs'  '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: ''
                UserData: []
          DimensionNames: {'Row'  'Variables'}
           VariableNames: {'PatientName'  'Age'  'Height'  'Weight'  'Smoker'  'BP'}
    VariableDescriptions: {'Patient last name'  ''  ''  ''  ''  'Systolic/Diastolic'}
           VariableUnits: {''  'Yrs'  '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: ''
                UserData: []
          DimensionNames: {'Row'  'Variables'}
           VariableNames: {'PatientName'  'Age'  'Height'  'Weight'  'Smoker'  'BP'}
    VariableDescriptions: {''  ''  ''  ''  ''  'Systolic/Diastolic'}
           VariableUnits: {''  'Yrs'  '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: ''
                UserData: []
          DimensionNames: {'Row'  'Variables'}
           VariableNames: {'PatientName'  'Age'  'Height'  'Weight'  'Smoker'  'BP'}
    VariableDescriptions: {}
           VariableUnits: {''  'Yrs'  'In'  'Lbs'  ''  'mm Hg'}
      VariableContinuity: []
                RowNames: {}
        CustomProperties: No custom properties are set.
      Use addprop and rmprop to modify CustomProperties.

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

プロパティと各変数に関する統計を組み合わせた、table の概要を生成できます。この概要を生成するには、関数 summary を使用します。

まず、table の説明とともに、変数の説明を T に追加します。

T.Properties.Description = "Table of Data for 100 Patients";
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: {''  'Yrs'  'In'  'Lbs'  ''  'mm Hg'}
      VariableContinuity: []
                RowNames: {}
        CustomProperties: No custom properties are set.
      Use addprop and rmprop to modify CustomProperties.

次に、summary を呼び出します。概要には、table の説明および各変数の説明と単位が表示されます。また、summary では、必要な計算をサポートするデータ型をもつ table 変数の統計も生成されます。

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

Variables:

    PatientName: 100x1 string

        Properties:
            Description:  Patient name
    Age: 100x1 double

        Properties:
            Units:  Yrs
        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]

参考

| |

関連するトピック