cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Submit your abstract to the call for content for Discovery Summit Americas by April 23. Selected abstracts will be presented at Discovery Summit, Oct. 21- 24.
Discovery is online this week, April 16 and 18. Join us for these exciting interactive sessions.
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?

1 ACCEPTED SOLUTION

Accepted Solutions
Jeff_Perkinson
Community Manager Community Manager

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

Tables->Summary is the way to create data tables that aggregate and summarize data. The Summary() message is the JSL way to create the data table.

2021-04-01_16-24-29.765.png

 

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

 

In contrast, the Summarize() function collects summary statistics for a data table and stores them in variables. This is useful if you need to use the summary statistics elsewhere in your script.

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]
-Jeff

View solution in original post

4 REPLIES 4
ms
Super User (Alumni) ms
Super User (Alumni)

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 ) );

 

 

mattf
Level V

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.
Jeff_Perkinson
Community Manager Community Manager

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

Tables->Summary is the way to create data tables that aggregate and summarize data. The Summary() message is the JSL way to create the data table.

2021-04-01_16-24-29.765.png

 

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

 

In contrast, the Summarize() function collects summary statistics for a data table and stores them in variables. This is useful if you need to use the summary statistics elsewhere in your script.

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]
-Jeff