How to bin a variable and create new variables for each bin

6 visualizzazioni (ultimi 30 giorni)
Anaya Kharwadkar il 8 Nov 2020
Commentato: Steven Lord il 12 Nov 2020
Name = [Alice Bob Cate Doug Ernest Fan]
Age = [23 15 24 32 44 22]
T = table(Name,Age)
I want to create bins for the variable Age: for eg: 0-10, 11-20, 21-30 and 31-40
Then I want to sort the whole table based on the bins.
Thus, I want to get 4 different tables based on the Age bins
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Risposte (2)

Mathieu NOE il 9 Nov 2020
hello
below an example for the first bin, easy to duplicate for the other ones
Name = [{'Alice'} {'Bob'} {'Cate'} {'Doug'} {'Ernest'} {'Fan'}];
Age = [23 5 24 32 44 22];
% I want to create bins for the variable Age: for eg: 0-10, 11-20, 21-30 and 31-40
% Then I want to sort the whole table based on the bins.
% Thus, I want to get 4 different tables based on the Age bins
% bin1
age_range1 = [0 10];
ind = find(Age>=age_range1(1) & Age<age_range1(2))
T = table(Name(ind),Age(ind))
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Eric Sofen il 12 Nov 2020
Modificato: Eric Sofen il 12 Nov 2020
I'd encourage you to consider whether you really need separate tables for each age bin or whether you can simply add a grouping variable (using discretize) to the table and then select subsets by group when you need to do further calculations. That will all you to be able to iterate over the groups in the table rather than needing to manage a bunch of workspace variables for the different bins.
1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Steven Lord il 12 Nov 2020
One way of how to create the grouping variable is to use discretize:
Name = ["Alice"; "Bob"; "Cate"; "Doug"; "Ernest"; "Fan"];
Age = [23; 15; 24; 32; 44; 22];
T = table(Name,Age)
T = 6x2 table
Name Age ________ ___ "Alice" 23 "Bob" 15 "Cate" 24 "Doug" 32 "Ernest" 44 "Fan" 22
AgeRange = discretize(T.Age, [0 11:10:51], 'categorical');
T.Class = AgeRange
T = 6x3 table
Name Age Class ________ ___ ________ "Alice" 23 [21, 31) "Bob" 15 [11, 21) "Cate" 24 [21, 31) "Doug" 32 [31, 41) "Ernest" 44 [41, 51] "Fan" 22 [21, 31)
Or if you want an identifier:
gr = discretize(T.Age, [0 11:10:51]);
T.AgeCategory = gr
T = 6x4 table
Name Age Class AgeCategory ________ ___ ________ ___________ "Alice" 23 [21, 31) 3 "Bob" 15 [11, 21) 2 "Cate" 24 [21, 31) 3 "Doug" 32 [31, 41) 4 "Ernest" 44 [41, 51] 5 "Fan" 22 [21, 31) 3

Accedi per commentare.

Categorie

Scopri di più su Tables in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by