# 拆分表数据变量并应用函数

### 加载电力中断数据

`T = readtable('outages.csv');`

`Region``Cause` 转换为分类数组，将 `OutageTime``RestorationTime` 转换为 `datetime` 数组。显示前五行。

```T.Region = categorical(T.Region); T.Cause = categorical(T.Cause); T.OutageTime = datetime(T.OutageTime); T.RestorationTime = datetime(T.RestorationTime); T(1:5,:)```
```ans=5×6 table Region OutageTime Loss Customers RestorationTime Cause _________ ________________ ______ __________ ________________ _______________ SouthWest 2002-02-01 12:18 458.98 1.8202e+06 2002-02-07 16:50 winter storm SouthEast 2003-01-23 00:49 530.14 2.1204e+05 NaT winter storm SouthEast 2003-02-07 21:15 289.4 1.4294e+05 2003-02-17 08:14 winter storm West 2004-04-06 05:44 434.81 3.4037e+05 2004-04-06 06:10 equipment fault MidWest 2002-03-16 06:18 186.44 2.1275e+05 2002-03-18 23:23 severe storm ```

### 计算最大电力损失

```G = findgroups(T.Region); maxLoss = splitapply(@max,T.Loss,G)```
```maxLoss = 5×1 104 × 2.3141 2.3418 0.8767 0.2796 1.6659 ```

```T1 = T(:,'Cause'); [G,powerLosses] = findgroups(T1); powerLosses.maxLoss = splitapply(@max,T.Loss,G)```
```powerLosses=10×2 table Cause maxLoss ________________ _______ attack 582.63 earthquake 258.18 energy emergency 11638 equipment fault 16659 fire 872.96 severe storm 8767.3 thunder storm 23418 unknown 23141 wind 2796 winter storm 2883.7 ```

`powerLosses` 是一个表，因为 `T1` 是一个表。您可以将最大损失附加为另一个表变量。

```T1 = T(:,{'Region','Cause'}); [G,powerLosses] = findgroups(T1); powerLosses.maxLoss = splitapply(@max,T.Loss,G); powerLosses(1:15,:)```
```ans=15×3 table Region Cause maxLoss _________ ________________ _______ MidWest attack 0 MidWest energy emergency 2378.7 MidWest equipment fault 903.28 MidWest severe storm 6808.7 MidWest thunder storm 15128 MidWest unknown 23141 MidWest wind 2053.8 MidWest winter storm 669.25 NorthEast attack 405.62 NorthEast earthquake 0 NorthEast energy emergency 11638 NorthEast equipment fault 794.36 NorthEast fire 872.96 NorthEast severe storm 6002.4 NorthEast thunder storm 23418 ```

### 计算受影响客户的数量

```osumFcn = @(x)(sum(x,'omitnan')); powerLosses.totalCustomers = splitapply(osumFcn,T.Customers,G); powerLosses(1:15,:)```
```ans=15×4 table Region Cause maxLoss totalCustomers _________ ________________ _______ ______________ MidWest attack 0 0 MidWest energy emergency 2378.7 6.3363e+05 MidWest equipment fault 903.28 1.7822e+05 MidWest severe storm 6808.7 1.3511e+07 MidWest thunder storm 15128 4.2563e+06 MidWest unknown 23141 3.9505e+06 MidWest wind 2053.8 1.8796e+06 MidWest winter storm 669.25 4.8887e+06 NorthEast attack 405.62 2181.8 NorthEast earthquake 0 0 NorthEast energy emergency 11638 1.4391e+05 NorthEast equipment fault 794.36 3.9961e+05 NorthEast fire 872.96 6.1292e+05 NorthEast severe storm 6002.4 2.7905e+07 NorthEast thunder storm 23418 2.1885e+07 ```

### 计算电力中断的平均持续时间

```D = T.RestorationTime - T.OutageTime; H = hours(D); omeanFcn = @(x)(mean(x,'omitnan')); powerLosses.meanOutage = splitapply(omeanFcn,H,G); powerLosses(1:15,:)```
```ans=15×5 table Region Cause maxLoss totalCustomers meanOutage _________ ________________ _______ ______________ __________ MidWest attack 0 0 335.02 MidWest energy emergency 2378.7 6.3363e+05 5339.3 MidWest equipment fault 903.28 1.7822e+05 17.863 MidWest severe storm 6808.7 1.3511e+07 78.906 MidWest thunder storm 15128 4.2563e+06 51.245 MidWest unknown 23141 3.9505e+06 30.892 MidWest wind 2053.8 1.8796e+06 73.761 MidWest winter storm 669.25 4.8887e+06 127.58 NorthEast attack 405.62 2181.8 5.5117 NorthEast earthquake 0 0 0 NorthEast energy emergency 11638 1.4391e+05 77.345 NorthEast equipment fault 794.36 3.9961e+05 87.204 NorthEast fire 872.96 6.1292e+05 4.0267 NorthEast severe storm 6002.4 2.7905e+07 2163.5 NorthEast thunder storm 23418 2.1885e+07 46.098 ```