Count frequency data from table
4 views (last 30 days)
Show older comments
I have a table T with two columns. I want to create two frequency tables from them for two groups - MLD and MOD. For these tables the rows would be 11, 12, 21, and 22. The columns would be P1, N12, P2, P3 and LPP. The frequnecy count would be filled in from the table below. is there a way to do this?
T =
MLD_HGH_CG_HGH_11_POR P3
MLD_HGH_CG_HGH_12_POL P1
MLD_HGH_CG_HGH_21_FCR P3
MLD_HGH_CG_HGH_22_CPL P1
MLD_HGH_CG_HGH_22_CPL P2
MOD_CG_11_CPR N1
MOD_CG_11_FCR N1
MOD_CG_12_CPL P2
MOD_CG_12_CPL P3
MOD_CG_12_CPL LPP
MOD_CG_12_CPR P3
Example of outcome for first 'MLD' table
P1 N1 P2 P3 LPP
11 0 0 0 1 0
12 1 0 0 0 0
21 0 0 0 1 0
22 1 0 1 0 0
0 Comments
Accepted Answer
Seth Furman
on 14 Jul 2021
This can be accomplished using groupsummary and unstack.
c = [ ...
"MLD_HGH_CG_HGH_11_POR" "P3"
"MLD_HGH_CG_HGH_12_POL" "P1"
"MLD_HGH_CG_HGH_21_FCR" "P3"
"MLD_HGH_CG_HGH_22_CPL" "P1"
"MLD_HGH_CG_HGH_22_CPL" "P2"
"MOD_CG_11_CPR" "N1"
"MOD_CG_11_FCR" "N1"
"MOD_CG_12_CPL" "P2"
"MOD_CG_12_CPL" "P3"
"MOD_CG_12_CPL" "LPP"
"MOD_CG_12_CPR" "P3"
];
t = table(c(:,1),c(:,2))
t.Group = extract(t.Var1,digitsPattern)
gs = groupsummary(t,["Group","Var2"],'IncludeEmptyGroups',true)
gs = unstack(gs,"GroupCount","Var2")
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!