Main Content

本页的翻译已过时。点击此处可查看最新英文版本。

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

此示例演示如何将表中的电力中断数据按电力中断的区域和原因拆分为不同的组。然后,它会显示如何应用函数以计算每个组的统计量并将结果收集到一个表中。

加载电力中断数据

示例文件 outages.csv 包含表示美国电力中断的数据。该文件包含六个列:RegionOutageTimeLossCustomersRestorationTimeCause。将 outages.csv 读入表中。

T = readtable('outages.csv');

RegionCause 转换为分类数组,将 OutageTimeRestorationTime 转换为 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   

计算最大电力损失

确定每个地区因电力中断而造成的最大电力损失。findgroups 函数会返回 G(从 T.Region 创建的组数目向量)。splitapply 函数会使用 GT.Loss 分为五个组,对应五个区域。splitapply 会将 max 函数应用于每个组并将最大电力损失串联到向量中。

G = findgroups(T.Region);
maxLoss = splitapply(@max,T.Loss,G)
maxLoss = 5×1
104 ×

    2.3141
    2.3418
    0.8767
    0.2796
    1.6659

计算因不同原因导致的电力中断而造成的最大电力损失。要指定 Cause 为分组变量,请使用表索引。创建一个包含最大电力损失及其原因的表。

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 是一个表。您可以将最大损失附加为另一个表变量。

计算每个地区因不同原因而造成的最大电力损失。要指定 RegionCause 为分组变量,请使用表索引。创建一个包含最大电力损失的表,并显示前 15 行。

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 

计算受影响客户的数量

确定不同原因和地区的电力中断对客户的影响。因为 T.Loss 包含 NaN 值,所以将 sum 打包在匿名函数中以使用 'omitnan' 输入参数。

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  

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

确定美国的所有电力中断的平均持续时间(以小时为单位)。将电力中断平均持续时间添加到 powerLosses。因为 T.RestorationTime 具有 NaT 值,所以在计算平均持续时间时请忽略生成的 NaN 值。

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  

另请参阅

| | |

相关示例

详细信息