Choose Language Hide Translation Bar

## How to transform matrix into 2D data table?

Input:
Summarize(g=by(age, sex), c=count());
show(g, c);
output:
g:{{"12", "12", "13", "13", "14", "14", "15", "15", "16", "16", "17",
"17"}, {"F", "M", "F", "M", "F", "M", "F", "M", "F", "M", "F", "M"}}
c:[5,3,3,4,5,7,2,5,2,1,1,2]

How to make the result into a data table with column as sex and row as age? Like pivot table?
3 REPLIES 3
Super User

## Re: How to transform matrix into 2D data table?

You can use e.g. for-loops to populate a data table with those results.

However, a more direct way to obtain a pivot table is to use the summary command using subgroups.
<--

Data Table( "Big Class" ) << Summary( Group( :age ), N, Subgroup( :sex ) );

!>
Community Trekker

## Re: How to transform matrix into 2D data table?

Please, are you perhaps looking for something as simple as a summary table of counts by age(rows) and sex(cols)? Example below. If more complicated please expand.

Best regards,
-Matt

## Re: How to transform matrix into 2D data table?

thanks. both are working. However, I need to have some other columns calulated based on the summary table and then put it into report. I think summary() might be more flexible.