<?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: Formula that counts the number of rows a given distance away with a specific value in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Formula-that-counts-the-number-of-rows-a-given-distance-away/m-p/914414#M107440</link>
    <description>&lt;P&gt;If you need a formula it can get a bit slow but using As Constant can help. You could take a look into Distance(), KDTable() and maybe VPTree().&amp;nbsp;&lt;/P&gt;
&lt;P&gt;These are not optimized but are some simple examples on how you can use As Constant() and KDTable/Distance&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Current Data Table() &amp;lt;&amp;lt; New Column("R", Numeric, Ordinal, Formula(
	As Constant(
		tree = KDTable(Current Data Table()[0, {"X", "Y"}]);
		{rows, dist} = tree &amp;lt;&amp;lt; K nearest rows(25, 13);
		v = rows[Loc(dist &amp;lt;= 2)];
	);
	If(Contains(v, Row()),
		1,
		0
	);
));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;One example using Distance&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Current Data Table() &amp;lt;&amp;lt; New Column("R", Numeric, Ordinal, Formula(
	As Constant(
		five_idx = Where(:Value == 5);
		m = :X[five_idx] || :Y[five_idx];
	);
	Sqrt(Distance(Matrix({:X, :Y})`, m)) &amp;lt;= 2;
));
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you do not need formula, these can be made more efficient&lt;/P&gt;</description>
    <pubDate>Thu, 20 Nov 2025 06:23:45 GMT</pubDate>
    <dc:creator>jthi</dc:creator>
    <dc:date>2025-11-20T06:23:45Z</dc:date>
    <item>
      <title>Formula that counts the number of rows a given distance away with a specific value</title>
      <link>https://community.jmp.com/t5/Discussions/Formula-that-counts-the-number-of-rows-a-given-distance-away/m-p/914404#M107439</link>
      <description>&lt;P&gt;I'm trying to develop a column function that will count every row of my data table where&lt;/P&gt;
&lt;P&gt;1) The :X and :Y coordinates are less than 2 Euclidean distance away&lt;BR /&gt;&amp;amp;&lt;BR /&gt;2) The :Value is = 5&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;In this 5 by 5 grid every :Value is set to 1, except for the central datapoint (3,3) which has :Value = 5.&lt;/P&gt;
&lt;P&gt;The datapoints in blue are &amp;lt;= 2 unites away from the central datapoint and thus get correctly count 1 datapoint in their range.&lt;/P&gt;
&lt;P&gt;The datapoints in red don't have any rows with :Value = 5&amp;nbsp;in range and thus count a value of zero.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Shawn_Noland_0-1763616723624.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/87388i95FB423486AD473E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Shawn_Noland_0-1763616723624.png" alt="Shawn_Noland_0-1763616723624.png" /&gt;&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;I've been experimenting with different methods to do this.&amp;nbsp; Mostly trying out the For Each Row() command, but that doesn't seem to work well in a column function.&amp;nbsp; &amp;nbsp;I'm probably doing it wrong.&amp;nbsp; &amp;nbsp;Any ideas?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Nov 2025 05:52:36 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Formula-that-counts-the-number-of-rows-a-given-distance-away/m-p/914404#M107439</guid>
      <dc:creator>Shawn_Noland</dc:creator>
      <dc:date>2025-11-20T05:52:36Z</dc:date>
    </item>
    <item>
      <title>Re: Formula that counts the number of rows a given distance away with a specific value</title>
      <link>https://community.jmp.com/t5/Discussions/Formula-that-counts-the-number-of-rows-a-given-distance-away/m-p/914414#M107440</link>
      <description>&lt;P&gt;If you need a formula it can get a bit slow but using As Constant can help. You could take a look into Distance(), KDTable() and maybe VPTree().&amp;nbsp;&lt;/P&gt;
&lt;P&gt;These are not optimized but are some simple examples on how you can use As Constant() and KDTable/Distance&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Current Data Table() &amp;lt;&amp;lt; New Column("R", Numeric, Ordinal, Formula(
	As Constant(
		tree = KDTable(Current Data Table()[0, {"X", "Y"}]);
		{rows, dist} = tree &amp;lt;&amp;lt; K nearest rows(25, 13);
		v = rows[Loc(dist &amp;lt;= 2)];
	);
	If(Contains(v, Row()),
		1,
		0
	);
));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;One example using Distance&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Current Data Table() &amp;lt;&amp;lt; New Column("R", Numeric, Ordinal, Formula(
	As Constant(
		five_idx = Where(:Value == 5);
		m = :X[five_idx] || :Y[five_idx];
	);
	Sqrt(Distance(Matrix({:X, :Y})`, m)) &amp;lt;= 2;
));
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you do not need formula, these can be made more efficient&lt;/P&gt;</description>
      <pubDate>Thu, 20 Nov 2025 06:23:45 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Formula-that-counts-the-number-of-rows-a-given-distance-away/m-p/914414#M107440</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2025-11-20T06:23:45Z</dc:date>
    </item>
  </channel>
</rss>

