How to create a pivot table from this table, Part 2

3 visualizaciones (últimos 30 días)
alpedhuez
alpedhuez el 21 de Ag. de 2021
Comentada: alpedhuez el 23 de Ag. de 2021
suppose I have a table T
location gender sales
-----------------------------------------------------------
Customer 1 NY male 10
Customer 2 LA female 20
Customer 3 Austin female 15
Customer 4 LA female 10
Then I want to create a pivot table
Male sales Female sales
--------------------------------------------
NY 10 0
LA 0 30
Austin 0 15
I checked unstack and grpstat but I am not sure. What will be a next step?

Respuesta aceptada

Stephen23
Stephen23 el 21 de Ag. de 2021
customer = {'Customer 1';'Customer 2';'Customer 3';'Customer 4'};
location = {'NY';'LA';'Austin';'LA'};
gender = {'male';'female';'female';'female'};
sales = [10;20;15;10];
T = table(customer,location,gender,sales)
T = 4×4 table
customer location gender sales ______________ __________ __________ _____ {'Customer 1'} {'NY' } {'male' } 10 {'Customer 2'} {'LA' } {'female'} 20 {'Customer 3'} {'Austin'} {'female'} 15 {'Customer 4'} {'LA' } {'female'} 10
S = removevars(T,'customer');
S = groupsummary(S,{'location','gender'},'sum')
S = 3×4 table
location gender GroupCount sum_sales __________ __________ __________ _________ {'Austin'} {'female'} 1 15 {'LA' } {'female'} 2 30 {'NY' } {'male' } 1 10
U = unstack(S,'sum_sales','gender')
U = 3×4 table
location GroupCount female male __________ __________ ______ ____ {'Austin'} 1 15 NaN {'LA' } 2 30 NaN {'NY' } 1 NaN 10
etc.

Más respuestas (0)

Categorías

Más información sobre Tables en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by