Merge Rows in Table with One Categorical Variable and One Numeric; Average the Numeric

조회 수: 7 (최근 30일)
My goal is to merge all of the categories into one, and to do so via taking the mean (this is confusing: look at the tables below, it's much easier to see what I want to do). It's already really tricky to find how to do this in MatLab, but it's even trickier because my categorical variable is an array.
I would like to take the following:
Categorical Variable Value
----------------------------------------------------
["A", "B"] 52
["A", "B"] 65
["C", "B"] 35
["W", "T"] 25
And get:
Categorical Variable Value
----------------------------------------------------
["A", "B"] 58.5
["C", "B"] 35
["W", "T"] 25
Notice how the first two rows merged, and did so on the "value" column by taking the mean.
If I do the following:
string(table.CategoricalVariable) == string(["A" "B"])
I get:
ans =
5×2 logical array
1 1
1 1
0 0
0 0
But I am, at this point, at a loss for how to do this concisely with that kind of ans. It feels like there should be an elegant way to do this?

답변 (1개)

dpb
dpb 2022년 6월 6일
편집: dpb 2022년 6월 6일
S=categorical([["A", "B"];["A", "B"];["C", "B"];["W", "T"]]);
V=[52;65;35;25];
tT=table(S,V);
[~,~,ia]=unique(tT.S,'rows');
tT.G=ia;
rowfun(@mean,tT,'GroupingVariables',{'G'},"InputVariables",{'V'})
ans =
3×3 table
G GroupCount Var3
_ __________ ____
1 2 58.5
2 1 35
3 1 25
>>
It would be simpler if you created the categorical variable as
>> S=categorical(join([["A", "B"];["A", "B"];["C", "B"];["W", "T"]]))
S =
4×1 categorical array
A B
A B
C B
W T
>>
instead if it is intended to be used as above; then it could be the grouping variable itself instead of creating another copy to use. groupsummary could be used instead with same caveat; you must create a grouping variable in some fashion; the simplest would be to make it a vector in the beginning. I've not come across a case where having more than a vector for a categorical variable was of use; I'm sure there's bound to be some place where that is the case, but it doesn't come to mind otomh what/how that would be. Mayhaps in 2D binning...

Community Treasure Hunt

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

Start Hunting!

Translated by