Calculations on Tables

This example shows how to perform calculation on tables.

The functions rowfun and varfun apply a specified function to a table, yet many other functions require numeric or homogeneous arrays as input arguments. You can extract data from individual variables using dot indexing or from one or more variables using curly braces. The extracted data is then an array that you can use as input to other functions.

Create and Load Sample Data

Create a comma-separated text file, testScores.csv, that contains the following data.

LastName,Gender,Test1,Test2,Test3
HOWARD,male,90,87,93
WARD,male,87,85,83
TORRES,male,86,85,88
PETERSON,female,75,80,72
GRAY,female,89,86,87
RAMIREZ,female,96,92,98
JAMES,male,78,75,77
WATSON,female,91,94,92
BROOKS,female,86,83,85
KELLY,male,79,76,82

Create a table from the comma-separated text file and use the unique identifiers in the first column as row names.

T = readtable('testScores.csv','ReadRowNames',true)
T = 

                 Gender     Test1    Test2    Test3
                --------    -----    -----    -----
    HOWARD      'male'      90       87       93   
    WARD        'male'      87       85       83   
    TORRES      'male'      86       85       88   
    PETERSON    'female'    75       80       72   
    GRAY        'female'    89       86       87   
    RAMIREZ     'female'    96       92       98   
    JAMES       'male'      78       75       77   
    WATSON      'female'    91       94       92   
    BROOKS      'female'    86       83       85   
    KELLY       'male'      79       76       82   

T is a table with 10 rows and 4 variables.

Summarize the Table

View the data type, description, units, and other descriptive statistics for each variable by using summary to summarize the table.

summary(T)
Variables:

    Gender: 10x1 cell string

    Test1: 10x1 double
        Values:
            min         75   
            median    86.5   
            max         96   

    Test2: 10x1 double
        Values:
            min       75     
            median    85     
            max       94     

    Test3: 10x1 double
        Values:
            min       72     
            median    86     
            max       98     

The summary contains the minimum, average, and maximum score for each test.

Find the Average Across Each Row

Extract the data from the second, third, and fourth variables using curly braces, {}, find the average of each row, and store it in a new variable, TestAvg.

T.TestAvg = mean(T{:,2:end},2)
T = 

                 Gender     Test1    Test2    Test3    TestAvg
                --------    -----    -----    -----    -------
    HOWARD      'male'      90       87       93           90 
    WARD        'male'      87       85       83           85 
    TORRES      'male'      86       85       88       86.333 
    PETERSON    'female'    75       80       72       75.667 
    GRAY        'female'    89       86       87       87.333 
    RAMIREZ     'female'    96       92       98       95.333 
    JAMES       'male'      78       75       77       76.667 
    WATSON      'female'    91       94       92       92.333 
    BROOKS      'female'    86       83       85       84.667 
    KELLY       'male'      79       76       82           79 

Alternatively, you can use the variable names, T{:,{'Test1','Test2','Test3'}} or the variable indices, T{:,2:4} to select the subset of data.

Compute Statistics Using a Grouping Variable

Compute the mean and maximum of TestAvg for each gender.

varfun(@mean,T,'InputVariables','TestAvg',...
    'GroupingVariables','Gender')
ans = 

               Gender     GroupCount    mean_TestAvg
              --------    ----------    ------------
    female    'female'    5             87.067      
    male      'male'      5               83.4      

Replace Data Values

The maximum score for each test is 100. Use curly braces to extract the data from the table and convert the test scores to a 25 point scale.

T{:,2:end} = T{:,2:end}*25/100
T = 

                 Gender     Test1    Test2    Test3    TestAvg
                --------    -----    -----    -----    -------
    HOWARD      'male'       22.5    21.75    23.25      22.5 
    WARD        'male'      21.75    21.25    20.75     21.25 
    TORRES      'male'       21.5    21.25       22    21.583 
    PETERSON    'female'    18.75       20       18    18.917 
    GRAY        'female'    22.25     21.5    21.75    21.833 
    RAMIREZ     'female'       24       23     24.5    23.833 
    JAMES       'male'       19.5    18.75    19.25    19.167 
    WATSON      'female'    22.75     23.5       23    23.083 
    BROOKS      'female'     21.5    20.75    21.25    21.167 
    KELLY       'male'      19.75       19     20.5     19.75 

Change a Variable Name

Change the variable name from TestAvg to Final.

T.Properties.VariableNames{end} = 'Final'
T = 

                 Gender     Test1    Test2    Test3    Final 
                --------    -----    -----    -----    ------
    HOWARD      'male'       22.5    21.75    23.25      22.5
    WARD        'male'      21.75    21.25    20.75     21.25
    TORRES      'male'       21.5    21.25       22    21.583
    PETERSON    'female'    18.75       20       18    18.917
    GRAY        'female'    22.25     21.5    21.75    21.833
    RAMIREZ     'female'       24       23     24.5    23.833
    JAMES       'male'       19.5    18.75    19.25    19.167
    WATSON      'female'    22.75     23.5       23    23.083
    BROOKS      'female'     21.5    20.75    21.25    21.167
    KELLY       'male'      19.75       19     20.5     19.75

See Also

| | | |

Related Examples

Was this topic helpful?