<?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: Have difficulty to find 2nd max, 3rd max value in 1 column etc in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Have-difficulty-to-find-2nd-max-3rd-max-value-in-1-column-etc/m-p/401534#M65240</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This depends on what you mean by "max", and whether you are willing to sort the table. I'll address a few common cases and hope one of them covers your use case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Case 1: "max" means position in sorted dataset of original values, including duplicates.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;You can use a built-in transform for this. Right-click on data in the column of interest and select New Formula Column &amp;gt; Distributional &amp;gt; Rank (Reverse Order).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Clicking on the formula that is created, we have the following, which works whether or not the table is sorted:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;(Col Number( :Col1 ) - Col Rank( :Col1 )) + 1&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="brady_brady_0-1626387795830.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/34199iE3375AAE5B00B5D6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="brady_brady_0-1626387795830.png" alt="brady_brady_0-1626387795830.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Case 2: "Max" means position in sorted set of UNIQUE values (no duplication). Unwilling to pre-sort data.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;***Note: due to its reliance on associative arrays, this approach only works with numeric data involving integers.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We can use the convoluted (and expensive) formula:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Loc(
	Matrix(
		Reverse(
			Associative Array( (:Col1 &amp;lt;&amp;lt; get data table) :: col1 &amp;lt;&amp;lt; getvalues ) &amp;lt;&amp;lt; get keys
		)
	),
	:Col1
)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This formula does the following:&lt;/P&gt;
&lt;P&gt;1) Gets the values from the column of interest and passes them to an Associative array, which removes duplicates.&lt;/P&gt;
&lt;P&gt;2) Gets the associative array keys (in order from low to high by default) and reverses their order.&lt;/P&gt;
&lt;P&gt;3) Uses the Loc() function to report the position of the given row within the matrix of reversed associative array keys.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Case 3: As in case 2, but we are willing to pre-sort (descending) by the column of interest.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this case we can use a simpler and more efficient, albeit self-referential formula:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If( Row() == 1, 
	1,
	Lag( :Formula3 ) + (Lag( :Col1 ) != :Col1)
)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Below, we see the difference between the first formula and the next two. Again, only the 3rd formula is dependent on table sorting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="brady_brady_4-1626389373876.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/34203i9559ACDA362CC9BA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="brady_brady_4-1626389373876.png" alt="brady_brady_4-1626389373876.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to try this out, run the script below. It first appears unsorted, but after a one-second pause, it sorts by col1, at which point the results of the 3rd formula will match the results of the second.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = New Table( "Example",
	Add Rows( 20 ),
	New Column( "Col1", &amp;lt;&amp;lt;set values(J(20,1,randominteger(1,12)))) ,
	New Column( "Reverse Rank[Col1]", formula((Col Number( :Col1 ) - Col Rank( :Col1 )) + 1)),
	New Column( "Formula2",
		Formula(
			Loc(
				Matrix(
					Reverse(
						Associative Array( (:Col1 &amp;lt;&amp;lt; get data table):col1  &amp;lt;&amp;lt; getvalues)
						 &amp;lt;&amp;lt; get keys
					)
				),
				:Col1
			)
		)
	),
	New Column( "Formula3",
		Formula( If( Row() == 1, 1, Lag( :Formula3 ) + (Lag( :Col1 ) != :Col1) ) )
	)
);
dt&amp;lt;&amp;lt; Run Formulas;
wait(1);
dt &amp;lt;&amp;lt; sort(by(:col1), order(descending), replacetable);&lt;/CODE&gt;&lt;/PRE&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;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 17 Jul 2021 02:47:46 GMT</pubDate>
    <dc:creator>brady_brady</dc:creator>
    <dc:date>2021-07-17T02:47:46Z</dc:date>
    <item>
      <title>Have difficulty to find 2nd max, 3rd max value in 1 column etc</title>
      <link>https://community.jmp.com/t5/Discussions/Have-difficulty-to-find-2nd-max-3rd-max-value-in-1-column-etc/m-p/401524#M65239</link>
      <description>&lt;P&gt;Hi JSL experts,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a big trouble to find the&amp;nbsp;2nd max, 3rd max etc value in 1 column.&lt;/P&gt;&lt;P&gt;For a col below, I hope to identify the top 5 max value, and mark the value out in a sperate column.&lt;/P&gt;&lt;P&gt;It seems I can only find the col max, but have a lot of difficulty to find the 2nd max, 3rd max etc.&lt;/P&gt;&lt;P&gt;Is there any standard formula for this solution?&lt;BR /&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(100,&lt;/P&gt;&lt;P&gt;232,&lt;/P&gt;&lt;P&gt;343,&lt;/P&gt;&lt;P&gt;121,&lt;/P&gt;&lt;P&gt;100,&lt;/P&gt;&lt;P&gt;132,&lt;/P&gt;&lt;P&gt;100,&lt;/P&gt;&lt;P&gt;100,&lt;/P&gt;&lt;P&gt;100,&lt;/P&gt;&lt;P&gt;100,)&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 19:52:39 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Have-difficulty-to-find-2nd-max-3rd-max-value-in-1-column-etc/m-p/401524#M65239</guid>
      <dc:creator>Stokes</dc:creator>
      <dc:date>2023-06-09T19:52:39Z</dc:date>
    </item>
    <item>
      <title>Re: Have difficulty to find 2nd max, 3rd max value in 1 column etc</title>
      <link>https://community.jmp.com/t5/Discussions/Have-difficulty-to-find-2nd-max-3rd-max-value-in-1-column-etc/m-p/401534#M65240</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This depends on what you mean by "max", and whether you are willing to sort the table. I'll address a few common cases and hope one of them covers your use case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Case 1: "max" means position in sorted dataset of original values, including duplicates.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;You can use a built-in transform for this. Right-click on data in the column of interest and select New Formula Column &amp;gt; Distributional &amp;gt; Rank (Reverse Order).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Clicking on the formula that is created, we have the following, which works whether or not the table is sorted:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;(Col Number( :Col1 ) - Col Rank( :Col1 )) + 1&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="brady_brady_0-1626387795830.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/34199iE3375AAE5B00B5D6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="brady_brady_0-1626387795830.png" alt="brady_brady_0-1626387795830.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Case 2: "Max" means position in sorted set of UNIQUE values (no duplication). Unwilling to pre-sort data.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;***Note: due to its reliance on associative arrays, this approach only works with numeric data involving integers.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We can use the convoluted (and expensive) formula:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Loc(
	Matrix(
		Reverse(
			Associative Array( (:Col1 &amp;lt;&amp;lt; get data table) :: col1 &amp;lt;&amp;lt; getvalues ) &amp;lt;&amp;lt; get keys
		)
	),
	:Col1
)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This formula does the following:&lt;/P&gt;
&lt;P&gt;1) Gets the values from the column of interest and passes them to an Associative array, which removes duplicates.&lt;/P&gt;
&lt;P&gt;2) Gets the associative array keys (in order from low to high by default) and reverses their order.&lt;/P&gt;
&lt;P&gt;3) Uses the Loc() function to report the position of the given row within the matrix of reversed associative array keys.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Case 3: As in case 2, but we are willing to pre-sort (descending) by the column of interest.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this case we can use a simpler and more efficient, albeit self-referential formula:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If( Row() == 1, 
	1,
	Lag( :Formula3 ) + (Lag( :Col1 ) != :Col1)
)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Below, we see the difference between the first formula and the next two. Again, only the 3rd formula is dependent on table sorting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="brady_brady_4-1626389373876.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/34203i9559ACDA362CC9BA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="brady_brady_4-1626389373876.png" alt="brady_brady_4-1626389373876.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to try this out, run the script below. It first appears unsorted, but after a one-second pause, it sorts by col1, at which point the results of the 3rd formula will match the results of the second.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = New Table( "Example",
	Add Rows( 20 ),
	New Column( "Col1", &amp;lt;&amp;lt;set values(J(20,1,randominteger(1,12)))) ,
	New Column( "Reverse Rank[Col1]", formula((Col Number( :Col1 ) - Col Rank( :Col1 )) + 1)),
	New Column( "Formula2",
		Formula(
			Loc(
				Matrix(
					Reverse(
						Associative Array( (:Col1 &amp;lt;&amp;lt; get data table):col1  &amp;lt;&amp;lt; getvalues)
						 &amp;lt;&amp;lt; get keys
					)
				),
				:Col1
			)
		)
	),
	New Column( "Formula3",
		Formula( If( Row() == 1, 1, Lag( :Formula3 ) + (Lag( :Col1 ) != :Col1) ) )
	)
);
dt&amp;lt;&amp;lt; Run Formulas;
wait(1);
dt &amp;lt;&amp;lt; sort(by(:col1), order(descending), replacetable);&lt;/CODE&gt;&lt;/PRE&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Jul 2021 02:47:46 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Have-difficulty-to-find-2nd-max-3rd-max-value-in-1-column-etc/m-p/401534#M65240</guid>
      <dc:creator>brady_brady</dc:creator>
      <dc:date>2021-07-17T02:47:46Z</dc:date>
    </item>
    <item>
      <title>Re: Have difficulty to find 2nd max, 3rd max value in 1 column etc</title>
      <link>https://community.jmp.com/t5/Discussions/Have-difficulty-to-find-2nd-max-3rd-max-value-in-1-column-etc/m-p/401552#M65245</link>
      <description>&lt;P&gt;Thanks a lot.&lt;/P&gt;&lt;P&gt;For case 1, It seems my JMP doesn't have Rank under Distributional, it only have the first 6 options as you showed in the pictures.&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Formula Column &amp;gt; Distributional &amp;gt; Rank (Reverse Order).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I think the case 3 seems a better solution for my current issue, appreciate the help.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Jul 2021 23:48:39 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Have-difficulty-to-find-2nd-max-3rd-max-value-in-1-column-etc/m-p/401552#M65245</guid>
      <dc:creator>Stokes</dc:creator>
      <dc:date>2021-07-15T23:48:39Z</dc:date>
    </item>
    <item>
      <title>Re: Have difficulty to find 2nd max, 3rd max value in 1 column etc</title>
      <link>https://community.jmp.com/t5/Discussions/Have-difficulty-to-find-2nd-max-3rd-max-value-in-1-column-etc/m-p/401560#M65247</link>
      <description>&lt;P&gt;You might be on an older version of JMP... does it work when you manually enter the formula I showed for that scenario? This may work for you--I can't recall when the by-variable support was added to the column functions... I think it may have been JMP 14 for a lot of them.&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;</description>
      <pubDate>Thu, 15 Jul 2021 23:59:36 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Have-difficulty-to-find-2nd-max-3rd-max-value-in-1-column-etc/m-p/401560#M65247</guid>
      <dc:creator>brady_brady</dc:creator>
      <dc:date>2021-07-15T23:59:36Z</dc:date>
    </item>
  </channel>
</rss>

