Main Content

테이블 변수 이름 바꾸기 및 설명하기

열 방향 변수에 데이터를 유지하는 테이블은 데이터에 대한 자세한 설명 정보를 저장할 수 있는 속성을 제공합니다. 예를 들어, 변수 이름은 속성에 저장되기 때문에 변수 이름을 자세한 설명이 담긴 이름으로 바꾸려면 테이블 속성을 변경하면 됩니다. 이 예제에서는 테이블 변수의 이름, 설명, 단위 등 테이블 속성에 액세스하고 변경하는 방법을 보여줍니다. 또한 이 예제에서는 테이블 변수의 데이터에 대한 통계량과 함께 이러한 속성을 보기 위해 테이블 요약을 생성하는 방법을 보여줍니다.

샘플 데이터에서 테이블 만들기

파일 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'}
    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'}

테이블 변수 이름 바꾸기

변수 이름이 설명적이면 아주 도움이 됩니다. 그런 이유로 테이블의 변수 이름을 바꾸고자 할 수 있습니다.

변수 이름을 바꾸려면 renamevars 함수를 사용하는 것이 좋습니다. 예를 들어, TLastName 변수 이름을 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
      ⋮

다른 속성 변경하기

다른 테이블 속성을 변경하려면 점 표기법을 사용해야 합니다. 일반적으로, 테이블이나 변수를 설명하는 정보를 테이블에 주석으로 추가하기 위해 다른 속성을 사용할 수 있습니다.

예를 들어, 테이블 전체에 대한 설명을 추가합니다. string형을 Description 속성에 할당합니다. 또한 테이블 변수와 연결된 단위를 추가합니다. 단위로 구성된 string형 배열을 VariableUnits 속성에 할당합니다. 속성이 문자형 벡터로 구성된 셀형 배열을 저장하는 경우에는 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.

속성의 요소를 참조하여 값을 할당할 수도 있습니다. 예를 들어, PatientNameBP 변수에 대한 설명만 추가해 보겠습니다. 테이블의 변수 이름 또는 변수 위치를 기준으로 인덱싱할 수 있습니다.

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.

속성값 삭제하기

테이블 속성은 삭제할 수 없습니다. 하지만 테이블 속성에 저장된 값은 삭제할 수 있습니다.

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

테이블 변수 데이터 및 속성 요약하기

테이블 속성과 함께 각 변수에 대한 통계량을 표시하는 테이블 요약을 생성할 수 있습니다. 이 요약을 생성하려면 summary 함수를 사용합니다. 요약에는 테이블 설명 및 각 변수에 대한 설명과 단위가 표시됩니다. 또한 필요한 계산을 지원하는 데이터형을 갖는 테이블 변수에 대한 통계량도 표시됩니다.

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]

참고 항목

| |

관련 항목