%% Split Table Data Variables and Apply Functions
% This example shows how to split power outage data from a table into groups by region and cause of the power outages. Then it shows how to apply functions to calculate statistics for each group and collect the results in a table.
%% Load Power Outage Data
% The sample file, outages.csv, contains data representing electric utility outages in the United States. The file contains six columns: Region, OutageTime,  Loss, Customers, RestorationTime, and Cause. Read outages.csv into a table.

T = readtable('outages.csv');

%
% Convert Region and Cause to categorical arrays, and OutageTime and  RestorationTime to datetime arrays. Display the first five rows.

T.Region = categorical(T.Region);
T.Cause = categorical(T.Cause);
T.OutageTime = datetime(T.OutageTime);
T.RestorationTime = datetime(T.RestorationTime);
T(1:5,:)

%
%% Calculate Maximum Power Loss
% Determine the greatest power loss due to a power outage in each region. The findgroups function returns G, a vector of group numbers created from  T.Region. The splitapply function uses G to split T.Loss into five groups, corresponding to the five regions. splitapply applies the max function to each group and concatenates the maximum power losses into a vector.

G = findgroups(T.Region);
maxLoss = splitapply(@max,T.Loss,G)

%
% Calculate the maximum power loss due to a power outage by cause. To specify that  Cause is the grouping variable, use table indexing. Create a table that contains the maximum power losses and their causes.

T1 = T(:,'Cause');
[G,powerLosses] = findgroups(T1);
powerLosses.maxLoss = splitapply(@max,T.Loss,G)

%
% powerLosses is a table because T1 is a table. You can append the maximum losses as another table variable.
% Calculate the maximum power loss by cause in each region. To specify that  Region and Cause are the grouping variables, use table indexing. Create a table that contains the maximum power losses and display the first 15 rows.

T1 = T(:,{'Region','Cause'});
[G,powerLosses] = findgroups(T1);
powerLosses.maxLoss = splitapply(@max,T.Loss,G);
powerLosses(1:15,:)

%
%% Calculate Number of Customers Impacted
% Determine power-outage impact on customers by cause and region. Because T.Loss contains NaN values, wrap sum in an anonymous function to use the 'omitnan' input argument.

osumFcn = @(x)(sum(x,'omitnan'));
powerLosses.totalCustomers = splitapply(osumFcn,T.Customers,G);
powerLosses(1:15,:)

%
%% Calculate Mean Durations of Power Outages
% Determine the mean durations of all U.S. power outages in hours. Add the mean durations of power outages to powerLosses. Because T.RestorationTime has NaT values, omit the resulting NaN values when calculating the mean durations.

D = T.RestorationTime - T.OutageTime;
H = hours(D);
omeanFcn = @(x)(mean(x,'omitnan'));
powerLosses.meanOutage = splitapply(omeanFcn,H,G);
powerLosses(1:15,:)

% Copyright 2015 The MathWorks, Inc.