How to create a pivot table from this table (revised)
Show older comments
Suppose I have a table T
location gender
-----------------------------------------------------------
Customer 1 NY male
Customer 2 LA female
Customer 3 Austin female
Customer 4 LA female
Then I want to create a pivot table
Male Female
--------------------------------------------
NY 1 0
LA 0 2
Austin 0 1
I checked unstack and grpstat but I am not sure. What will be a next step?
Accepted Answer
More Answers (1)
What you want to do is not really a pivot:
% Create your table
customer = {'1';'2';'3'};
location = {'NY';'LA';'Austin'};
gender = {'male';'female';'female'};
tbl = table(customer,location,gender)
% You don't need the above. It is just to create your data.
% Create columns for male and female
tbl.male = double(strcmp(tbl.gender,'male'));
tbl.female = double(strcmp(tbl.gender,'female'));
% Drop the gender column
tbl.gender = []
3 Comments
the cyclist
on 15 Aug 2021
Ah. I thought you just wanted to replace male/female with binary variables.
alpedhuez
on 15 Aug 2021
Categories
Find more on Resampling Techniques in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!