<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to use JSL to classify and summarize the data in the middle of the matrix branch, and get the summary results in the form of matrix? in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/How-to-use-JSL-to-classify-and-summarize-the-data-in-the-middle/m-p/381397#M63190</link>
    <description>&lt;P&gt;EDIT:&lt;/P&gt;
&lt;P&gt;I noticed a typo in the original version of the code below, which I have corrected. In the last line, the typo was:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;nrow (s)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead, use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;nrow (h)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course, nrow (n) or nrow(m) would also work since each of the vectors h, n and m have the same number of rows.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sorry about that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Brady&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/////////////////////////////Original post:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In looking into the performance of the table-based approach, it is much slower than matrix-based approaches, which might offset its simplicity, depending on your table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is another matrix-based approach; while it performs similarly to the one Ian posted, I include it for a few reasons: a) to introduce the idea of a "selected" column, which flags 1 if a row is selected and 0 otherwise, b) to show a "hack" of the summarize function and c) because this avoids looping and the potential indexing pitfalls associated therewith.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First: the "Selected" column. In the first line of the code below, we add a new column to the table, which sets itself to 1 in selected rows, and 0 elsewhere. This allows us to create an additional grouping column, with the goal of using table-based platforms and functions like Summary and Summarize, without having to create new tables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next, the Summarize () hack: note that we group by :selected and :height. While the h, n and m values are returned in matrices, the by variable(s) values are placed in lists of strings... even if they COULD be placed into matrices. So in this case, b is a list of two lists of strings. Thus, if we want any of these values in matrix form, we can either convert the desired list of strings into a matrix (which is expensive if the list is large) or we can compute a "cheap" statistic, like Min or Max, on the very same column, to get those values... which is what we do below for the height column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Brady&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt &amp;lt;&amp;lt; New Column( "Selected", formula( Selected( Row State() ) ) );

dt &amp;lt;&amp;lt; Select Rows( 6 :: 35 );

Summarize( dt, b = by( :Selected, :height ), h = max( :height), n = Count( :weight ), m = Mean( :weight ) ); 

sumMat = (h || n || m)[contains(b[1], "1"):: nrow(h),0];  //only use the results where selected == 1


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 03 May 2021 12:22:17 GMT</pubDate>
    <dc:creator>brady_brady</dc:creator>
    <dc:date>2021-05-03T12:22:17Z</dc:date>
    <item>
      <title>How to use JSL to classify and summarize the data in the middle of the matrix branch, and get the summary results in the form of matrix?</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-use-JSL-to-classify-and-summarize-the-data-in-the-middle/m-p/381196#M63172</link>
      <description>&lt;P&gt;For "Big Class.jmp", the "height" and "weight" columns are written into the matrix.&lt;BR /&gt;Then the data in rows 6 to 35 of the matrix are classified and summarized: the average weight of each height is summarized.This result only needs to be stored in the in-memory matrix.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2021-04-30_10-53-12.png" style="width: 727px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/32463i5FBFC5F838F80F88/image-size/large?v=v2&amp;amp;px=999" role="button" title="2021-04-30_10-53-12.png" alt="2021-04-30_10-53-12.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks Experts!&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;P class="src highlight"&gt;I wonder if this is possible?&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt=Open("$SAMPLE_DATA/Big Class.jmp");
ar=dt&amp;lt;&amp;lt;GetAsMatrix({4,5});
Summarize(ar[6::35,0],exg=By(?,?),exm=sum(?));&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 19:45:48 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-use-JSL-to-classify-and-summarize-the-data-in-the-middle/m-p/381196#M63172</guid>
      <dc:creator>lwx228</dc:creator>
      <dc:date>2023-06-09T19:45:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to use JSL to classify and summarize the data in the middle of the matrix branch, and get the summary results in the form of matrix?</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-use-JSL-to-classify-and-summarize-the-data-in-the-middle/m-p/381205#M63173</link>
      <description>&lt;P&gt;I'm not sure it's the best way to get your end result, but if you wanted to do it using just matrices you can:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;NamesDefaultToHere(1);
// Sample data
dt = Open("$SAMPLE_DATA/Big Class.jmp");
// Matrix with all 'height' and 'weight' values
ar = dt &amp;lt;&amp;lt; GetAsMatrix({4,5});
// Discard rows
ar = ar[6::35, 0];
// Find out the distinct levels of 'height'
hl = Matrix(AssociativeArray(ar[0,1]) &amp;lt;&amp;lt; getKeys);
// Build the average 'weight' for each level into a new matrix 'rm'
rm = J(NRow(hl), 2, .);
for(h=1, h&amp;lt;=NRow(hl), h++,
	rm[h, 1] = hl[h];
	rm[h, 2] = VMean(ar[Loc(ar[0, 1] == hl[h]), 2]);
	);
Print(rm);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 30 Apr 2021 10:39:28 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-use-JSL-to-classify-and-summarize-the-data-in-the-middle/m-p/381205#M63173</guid>
      <dc:creator>ian_jmp</dc:creator>
      <dc:date>2021-04-30T10:39:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to use JSL to classify and summarize the data in the middle of the matrix branch, and get the summary results in the form of matrix?</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-use-JSL-to-classify-and-summarize-the-data-in-the-middle/m-p/381293#M63178</link>
      <description>&lt;P&gt;Thank ian! This is the way this cycle works.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;ar=dt&amp;lt;&amp;lt;GetAsMatrix({4,5});ar=ar[6::35,0];
hl=Matrix(AssociativeArray(ar[0,1])&amp;lt;&amp;lt;getKeys);rm=J(NRow(hl),2,.);
for(h=2,h&amp;lt;=NRow(hl)-1,h++,
	rm[h,1]=hl[h];
	rm[h,2]=Vsum(ar[Loc(ar[0,1]==hl[h-1]),2])+Vsum(ar[Loc(ar[0,1]==hl[h]),2])+Vsum(ar[Loc(ar[0,1]==hl[h+1]),2]);
	);
r=rm[contains(rm[0,2],max(rm[0,2])),1];&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 30 Apr 2021 13:57:42 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-use-JSL-to-classify-and-summarize-the-data-in-the-middle/m-p/381293#M63178</guid>
      <dc:creator>lwx228</dc:creator>
      <dc:date>2021-04-30T13:57:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to use JSL to classify and summarize the data in the middle of the matrix branch, and get the summary results in the form of matrix?</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-use-JSL-to-classify-and-summarize-the-data-in-the-middle/m-p/381358#M63184</link>
      <description>&lt;P&gt;Here is an approach that avoids looping, although it creates invisible tables. If the performance is adequate for your needs, this approach is simple to use and to understand.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Brady&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = open("$Sample_Data/Big Class.jmp");

dtSub = dt &amp;lt;&amp;lt; subset(rows(6::35), invisible);

dtsum = dtsub &amp;lt;&amp;lt; summary(group(:height), mean(weight), invisible);

mat = dtSum &amp;lt;&amp;lt; get as Matrix;

close(dtSum, nosave);
close(dtSub, nosave);

show(mat);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 30 Apr 2021 16:23:30 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-use-JSL-to-classify-and-summarize-the-data-in-the-middle/m-p/381358#M63184</guid>
      <dc:creator>brady_brady</dc:creator>
      <dc:date>2021-04-30T16:23:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to use JSL to classify and summarize the data in the middle of the matrix branch, and get the summary results in the form of matrix?</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-use-JSL-to-classify-and-summarize-the-data-in-the-middle/m-p/381397#M63190</link>
      <description>&lt;P&gt;EDIT:&lt;/P&gt;
&lt;P&gt;I noticed a typo in the original version of the code below, which I have corrected. In the last line, the typo was:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;nrow (s)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead, use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;nrow (h)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course, nrow (n) or nrow(m) would also work since each of the vectors h, n and m have the same number of rows.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sorry about that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Brady&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/////////////////////////////Original post:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In looking into the performance of the table-based approach, it is much slower than matrix-based approaches, which might offset its simplicity, depending on your table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is another matrix-based approach; while it performs similarly to the one Ian posted, I include it for a few reasons: a) to introduce the idea of a "selected" column, which flags 1 if a row is selected and 0 otherwise, b) to show a "hack" of the summarize function and c) because this avoids looping and the potential indexing pitfalls associated therewith.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First: the "Selected" column. In the first line of the code below, we add a new column to the table, which sets itself to 1 in selected rows, and 0 elsewhere. This allows us to create an additional grouping column, with the goal of using table-based platforms and functions like Summary and Summarize, without having to create new tables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next, the Summarize () hack: note that we group by :selected and :height. While the h, n and m values are returned in matrices, the by variable(s) values are placed in lists of strings... even if they COULD be placed into matrices. So in this case, b is a list of two lists of strings. Thus, if we want any of these values in matrix form, we can either convert the desired list of strings into a matrix (which is expensive if the list is large) or we can compute a "cheap" statistic, like Min or Max, on the very same column, to get those values... which is what we do below for the height column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Brady&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt &amp;lt;&amp;lt; New Column( "Selected", formula( Selected( Row State() ) ) );

dt &amp;lt;&amp;lt; Select Rows( 6 :: 35 );

Summarize( dt, b = by( :Selected, :height ), h = max( :height), n = Count( :weight ), m = Mean( :weight ) ); 

sumMat = (h || n || m)[contains(b[1], "1"):: nrow(h),0];  //only use the results where selected == 1


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 May 2021 12:22:17 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-use-JSL-to-classify-and-summarize-the-data-in-the-middle/m-p/381397#M63190</guid>
      <dc:creator>brady_brady</dc:creator>
      <dc:date>2021-05-03T12:22:17Z</dc:date>
    </item>
  </channel>
</rss>

