Monthly data to seasonal using sum (how to use splitapply)

Hi, I wish you are safe and healthy!
I have a 1 x 3 cell with three 360 x 3 table. Each table has a column date with 30 years of monthly data (30 x 12 = 360). I want to have data for seasonal scale, winter, spring, summer and autumn correspond to December–February, March-May, June–August, and September–November, respectively using sum. I would like to create each season in a separate column with the corresponding name of the season and located after all existing columns in the tables.
Thank you so much

 Accepted Answer

Try below
tbl = CELL{1};
tbl.month = month(tbl.dates);
tbl.season = floor(tbl.month ./ 3);
tbl.season(tbl.season == 4) = 0;
tbl.season = categorical(tbl.season, [0 1 2 3], ["Spr", "Sum", "Aut", "Win"]);
You can use findgroups to group them based on seasons and use varfun or splitapply to calculate sum.

5 Comments

Thank you dear Peng Li,
I try this and then read findgroups documantation and using this line in order to get sum of spring seasons:
season = findgroups(tbl.season);
splitapply(@sum,season,tbl)
But it doesnt work and gave me this error:
Error using splitapply (line 61)
Group numbers must be a vector of positive integers, and cannot be a
sparse vector.
If you please could you show me an example for one season?
Thank you.
The second parameter of splitapply should be the table and group parameter should be on the third place if I remember correctly.
will have an example later. Using cellphone now.
>> [grp, meanTbl] = findgroups(tbl(:, 'season'));
>> meanTbl.rrr24 = splitapply(@mean, tbl.rrr24, grp);
>> meanTbl
meanTbl =
4×2 table
season rrr24
______ ______
Spr 14.038
Sum 35.004
Aut 10.949
Win 16.958

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Release

R2018b

Asked:

BN
on 28 Mar 2020

Commented:

BN
on 30 Mar 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!