# 表数据计算

### 将样本数据读入到表中

```scores = readtable("testScores.csv","TextType","string"); scores.School = categorical(scores.School)```
```scores=10×5 table LastName School Test1 Test2 Test3 __________ __________ _____ _____ _____ "Jeong" XYZ School 90 87 93 "Collins" XYZ School 87 85 83 "Torres" XYZ School 86 85 88 "Phillips" ABC School 75 80 72 "Ling" ABC School 89 86 87 "Ramirez" ABC School 96 92 98 "Lee" XYZ School 78 75 77 "Walker" ABC School 91 94 92 "Garcia" ABC School 86 83 85 "Chang" XYZ School 79 76 82 ```

### 创建包含数值数据的子表

`numericScores = scores(:,3:end)`
```numericScores=10×3 table Test1 Test2 Test3 _____ _____ _____ 90 87 93 87 85 83 86 85 88 75 80 72 89 86 87 96 92 98 78 75 77 91 94 92 86 83 85 79 76 82 ```

`numericVars = vartype("numeric")`
```numericVars = table vartype subscript: Select table variables matching the type 'numeric' See Access Data in a Table. ```
`numericScores = scores(:,numericVars)`
```numericScores=10×3 table Test1 Test2 Test3 _____ _____ _____ 90 87 93 87 85 83 86 85 88 75 80 72 89 86 87 96 92 98 78 75 77 91 94 92 86 83 85 79 76 82 ```

### 对表数据执行算术运算和调用函数

`A = numericScores{:,:}`
```A = 10×3 90 87 93 87 85 83 86 85 88 75 80 72 89 86 87 96 92 98 78 75 77 91 94 92 86 83 85 79 76 82 ```

`numericScores{:,:} = numericScores{:,:} * 0.25`
```numericScores=10×3 table Test1 Test2 Test3 _____ _____ _____ 22.5 21.75 23.25 21.75 21.25 20.75 21.5 21.25 22 18.75 20 18 22.25 21.5 21.75 24 23 24.5 19.5 18.75 19.25 22.75 23.5 23 21.5 20.75 21.25 19.75 19 20.5 ```

`numericScores.Variables = numericScores.Variables - min(numericScores.Variables)`
```numericScores=10×3 table Test1 Test2 Test3 _____ _____ _____ 3.75 3 5.25 3 2.5 2.75 2.75 2.5 4 0 1.25 0 3.5 2.75 3.75 5.25 4.25 6.5 0.75 0 1.25 4 4.75 5 2.75 2 3.25 1 0.25 2.5 ```

`numericScores.Test3 = numericScores.Test3 + 5`
```numericScores=10×3 table Test1 Test2 Test3 _____ _____ _____ 3.75 3 10.25 3 2.5 7.75 2.75 2.5 9 0 1.25 5 3.5 2.75 8.75 5.25 4.25 11.5 0.75 0 6.25 4 4.75 10 2.75 2 8.25 1 0.25 7.5 ```

### 对每个表行进行计算

```vars = ["Test1","Test2","Test3"]; scores.TestMean = mean(scores{:,vars},2)```
```scores=10×6 table LastName School Test1 Test2 Test3 TestMean __________ __________ _____ _____ _____ ________ "Jeong" XYZ School 90 87 93 90 "Collins" XYZ School 87 85 83 85 "Torres" XYZ School 86 85 88 86.333 "Phillips" ABC School 75 80 72 75.667 "Ling" ABC School 89 86 87 87.333 "Ramirez" ABC School 96 92 98 95.333 "Lee" XYZ School 78 75 77 76.667 "Walker" ABC School 91 94 92 92.333 "Garcia" ABC School 86 83 85 84.667 "Chang" XYZ School 79 76 82 79 ```

```minmaxTest = rowfun(@bounds, ... scores, ... "InputVariables",vars, ... "OutputVariableNames",["TestMin","TestMax"], ... "SeparateInputs",false)```
```minmaxTest=10×2 table TestMin TestMax _______ _______ 87 93 83 87 85 88 72 80 86 89 92 98 75 78 91 94 83 86 76 82 ```

`scores = [scores minmaxTest]`
```scores=10×8 table LastName School Test1 Test2 Test3 TestMean TestMin TestMax __________ __________ _____ _____ _____ ________ _______ _______ "Jeong" XYZ School 90 87 93 90 87 93 "Collins" XYZ School 87 85 83 85 83 87 "Torres" XYZ School 86 85 88 86.333 85 88 "Phillips" ABC School 75 80 72 75.667 72 80 "Ling" ABC School 89 86 87 87.333 86 89 "Ramirez" ABC School 96 92 98 95.333 92 98 "Lee" XYZ School 78 75 77 76.667 75 78 "Walker" ABC School 91 94 92 92.333 91 94 "Garcia" ABC School 86 83 85 84.667 83 86 "Chang" XYZ School 79 76 82 79 76 82 ```

### 对每个表变量进行计算

```vars = ["Test1","Test2","Test3"]; meanOfEachTest = mean(scores{:,vars})```
```meanOfEachTest = 1×3 85.7000 84.3000 85.7000 ```

```meanOfEachTest = varfun(@mean, ... scores, ... "InputVariables",vars)```
```meanOfEachTest=1×3 table mean_Test1 mean_Test2 mean_Test3 __________ __________ __________ 85.7 84.3 85.7 ```

### 使用表中的数据组进行计算

```vars = ["Test1","Test2","Test3"]; meanScoresBySchool = varfun(@mean, ... scores, ... "InputVariables",vars, ... "GroupingVariables","School")```
```meanScoresBySchool=2×5 table School GroupCount mean_Test1 mean_Test2 mean_Test3 __________ __________ __________ __________ __________ ABC School 5 87.4 87 86.8 XYZ School 5 84 81.6 84.6 ```

`meanScoresBySchool = groupsummary(scores,"School","mean",vars)`
```meanScoresBySchool=2×5 table School GroupCount mean_Test1 mean_Test2 mean_Test3 __________ __________ __________ __________ __________ ABC School 5 87.4 87 86.8 XYZ School 5 84 81.6 84.6 ```

`groupsummary` 的语法稍微简单一些。此外，您可以使用 `groupsummary` 一次指定多个方法。例如，按学校找出每次考试的最低分和最高分。

`minmaxBySchool = groupsummary(scores,"School",["min","max"],vars)`
```minmaxBySchool=2×8 table School GroupCount min_Test1 max_Test1 min_Test2 max_Test2 min_Test3 max_Test3 __________ __________ _________ _________ _________ _________ _________ _________ ABC School 5 75 96 80 94 72 98 XYZ School 5 78 90 75 87 77 93 ```

`allStatsBySchool = groupsummary(scores,"School","all","TestMean")`
```allStatsBySchool=2×13 table School GroupCount mean_TestMean sum_TestMean min_TestMean max_TestMean range_TestMean median_TestMean mode_TestMean var_TestMean std_TestMean nummissing_TestMean nnz_TestMean __________ __________ _____________ ____________ ____________ ____________ ______________ _______________ _____________ ____________ ____________ ___________________ ____________ ABC School 5 87.067 435.33 75.667 95.333 19.667 87.333 75.667 57.967 7.6136 0 5 XYZ School 5 83.4 417 76.667 90 13.333 85 76.667 29.856 5.464 0 5 ```

```maxScoresBySchool = rowfun(@findNameAtMax, ... scores, ... "InputVariables",["LastName","TestMean"], ... "GroupingVariables","School", ... "OutputVariableNames",["max_TestMean","LastName"])```
```maxScoresBySchool=2×4 table School GroupCount max_TestMean LastName __________ __________ ____________ _________ ABC School 5 95.333 "Ramirez" XYZ School 5 90 "Jeong" ```

### 辅助函数

```function [maxValue,lastName] = findNameAtMax(names,values) % Return maximum value and the last name % from the row at which the maximum value occurred [maxValue,maxIndex] = max(values); lastName = names(maxIndex); end```