Documentation

Calculations on Tables

This example shows how to perform calculations on tables.

The functionsrowfunandvarfuneach 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.

Read Sample Data into Table

Read data from a comma-separated text file,testScores.csv, into a table using thereadtablefunction.testScores.csvcontains test scores for several students. Use the student names in the first column of the text file as row names in the table.

T = readtable('testScores.csv','ReadRowNames',真正的)
T=10×4 tableGender 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

Tis a table with 10 rows and four variables.

Summarize the Table

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

summary(T)
Variables: Gender: 10x1 cell array of character vectors 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, median, 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=10×5 tableGender 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 Grouping Variable

Compute the mean and maximum ofTestAvgby gender of the students.

varfun(@mean,T,'InputVariables','TestAvg',...'GroupingVariables','Gender')
ans=2×3 tableGender GroupCount mean_TestAvg __________ __________ ____________ {'female'} 5 87.067 {'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=10×5 tableGender 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 Variable Name

Change the variable name fromTestAvgtoFinal.

T.Properties.VariableNames{end} ='Final'
T=10×5 tableGender 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 Topics