<?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: Calculate Column Difference for other column levels where lag varies in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Calculate-Column-Difference-for-other-column-levels-where-lag/m-p/780355#M96233</link>
    <description>&lt;P&gt;This approach worked great, thanks!&lt;/P&gt;</description>
    <pubDate>Thu, 08 Aug 2024 18:15:49 GMT</pubDate>
    <dc:creator>ashwint27</dc:creator>
    <dc:date>2024-08-08T18:15:49Z</dc:date>
    <item>
      <title>Calculate Column Difference for other column levels where lag varies</title>
      <link>https://community.jmp.com/t5/Discussions/Calculate-Column-Difference-for-other-column-levels-where-lag/m-p/780311#M96229</link>
      <description>&lt;P&gt;I have a column :EVENT (character-nominal) with various values.&amp;nbsp; Also another column :datetime (numeric-continuous).&amp;nbsp; I have :datetime column sorted ascending.&amp;nbsp; I am interested in having another column calculate for whenever there is a "CONNECT" event, what is the time difference between it and the last "DISCONNECT" event.&amp;nbsp; The issue is that the lag can vary between a CONNECT and previous DISCONNECT (see photo below) and I'd also prefer keeping this tall table format.&amp;nbsp; A solution for datetime sorted-ascending is appreciated, but also not sorted would also be beneficial.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ashwint27_0-1723138090598.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/66979iED1AA6A4B694D32C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ashwint27_0-1723138090598.png" alt="ashwint27_0-1723138090598.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Aug 2024 17:38:20 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Calculate-Column-Difference-for-other-column-levels-where-lag/m-p/780311#M96229</guid>
      <dc:creator>ashwint27</dc:creator>
      <dc:date>2024-08-08T17:38:20Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Column Difference for other column levels where lag varies</title>
      <link>https://community.jmp.com/t5/Discussions/Calculate-Column-Difference-for-other-column-levels-where-lag/m-p/780342#M96230</link>
      <description>&lt;P&gt;You can for example use variable in the formula to store the value of last disconnect&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

dt = New Table("Untitled",
	Add Rows(12),
	Compress File When Saved(1),
	New Column("E", Character, "Nominal", Set Values({"A", "B", "Disc", "Con", "A", "B", "C", "B", "Disc", "A", "B", "Con"})),
	New Column("T", Numeric, "Continuous", Format("Best", 12), Set Values([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]))
);

dt &amp;lt;&amp;lt; New Column("Dif", Numeric, Continuous, Formula(
	If(Row() == 1,
		last_disc = .;
		retval = .;
	);
	If(:E == "Disc",
		last_disc = :T;
		retval = .;
	, :E == "Con",
		retval = :T - last_disc
	,
		retval = .;
	);
	retval;
));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jthi_0-1723139562191.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/66980iE0E3A272610289BA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jthi_0-1723139562191.png" alt="jthi_0-1723139562191.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Aug 2024 17:53:05 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Calculate-Column-Difference-for-other-column-levels-where-lag/m-p/780342#M96230</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2024-08-08T17:53:05Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Column Difference for other column levels where lag varies</title>
      <link>https://community.jmp.com/t5/Discussions/Calculate-Column-Difference-for-other-column-levels-where-lag/m-p/780355#M96233</link>
      <description>&lt;P&gt;This approach worked great, thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 08 Aug 2024 18:15:49 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Calculate-Column-Difference-for-other-column-levels-where-lag/m-p/780355#M96233</guid>
      <dc:creator>ashwint27</dc:creator>
      <dc:date>2024-08-08T18:15:49Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Column Difference for other column levels where lag varies</title>
      <link>https://community.jmp.com/t5/Discussions/Calculate-Column-Difference-for-other-column-levels-where-lag/m-p/780359#M96235</link>
      <description>&lt;P&gt;You can also use matrices&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

dt = New Table("Untitled",
	Add Rows(12),
	Compress File When Saved(1),
	New Column("E", Character, "Nominal", Set Values({"A", "B", "Disc", "Con", "A", "B", "C", "B", "Disc", "A", "B", "Con"})),
	New Column("T", Numeric, "Continuous", Format("Best", 12), Set Values([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]))
);

events = :E &amp;lt;&amp;lt; get values;
times = :T &amp;lt;&amp;lt; get values;

discs = Loc(events, "Disc");
cons = Loc(events, "Con");

difs = J(1, N Rows(dt), .);
difs[cons] = times[cons] - times[discs];

dt &amp;lt;&amp;lt; new column("difs", numeric, continuous, values(difs));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This does have quite a few assumptions but can be made more robust for all sorts of edge cases (Loc Sorted can be helpful for something like this, sometimes even KDTable/VPTree).&lt;/P&gt;</description>
      <pubDate>Thu, 08 Aug 2024 18:23:00 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Calculate-Column-Difference-for-other-column-levels-where-lag/m-p/780359#M96235</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2024-08-08T18:23:00Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Column Difference for other column levels where lag varies</title>
      <link>https://community.jmp.com/t5/Discussions/Calculate-Column-Difference-for-other-column-levels-where-lag/m-p/781399#M96420</link>
      <description>&lt;P&gt;To throw a slight wrinkle into this, there are times where a CONNECT Event may show up a few times before a DISCONNECT Event comes up.&amp;nbsp; In this case, I'd like the difference to only be calculated on the &lt;EM&gt;first&lt;/EM&gt; CONNECT Event that appears after a DISCONNECT Event.&amp;nbsp; Is there a slight modification to the script that can accommodate this?&amp;nbsp; Thanks again for your help.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Aug 2024 20:48:51 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Calculate-Column-Difference-for-other-column-levels-where-lag/m-p/781399#M96420</guid>
      <dc:creator>ashwint27</dc:creator>
      <dc:date>2024-08-13T20:48:51Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Column Difference for other column levels where lag varies</title>
      <link>https://community.jmp.com/t5/Discussions/Calculate-Column-Difference-for-other-column-levels-where-lag/m-p/781401#M96421</link>
      <description>&lt;P&gt;Here is a modification to&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/14366"&gt;@jthi&lt;/a&gt;&amp;nbsp;code that should handle this.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

dt = New Table("Untitled",
	Add Rows(12),
	Compress File When Saved(1),
	New Column("E", Character, "Nominal", Set Values({"A", "B", "Disc", "Con", "A", "B", "C", "B", "Disc", "A", "B", "Con"})),
	New Column("T", Numeric, "Continuous", Format("Best", 12), Set Values([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]))
);

dt &amp;lt;&amp;lt; New Column("Dif", Numeric, Continuous, Formula(
	If(Row() == 1,
		last_disc = .;
		retval = .;
	);
	If(:E == "Disc",
		last_disc = :T;
		retval = .;
		firstCon = .;
	, :E == "Con",
		If(ismissing(firstcon), firstcon=:t);
		retval = firstcon - last_disc
	,
		retval = .;
	);
	retval;
));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp; I suggest you take the time to read the Scripting Guide, available under JMP Help.&amp;nbsp; Also please take the time to study the code provided by responding Discussion Forum members.&amp;nbsp; The modifications to Jarmo's script did not require any new scripting knowledge.&amp;nbsp; It only required some additional logic added to the flow of the script.&amp;nbsp; It should be a user's goal to be able to make their own enhancements to the scripts&lt;/P&gt;</description>
      <pubDate>Tue, 13 Aug 2024 21:37:28 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Calculate-Column-Difference-for-other-column-levels-where-lag/m-p/781401#M96421</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2024-08-13T21:37:28Z</dc:date>
    </item>
  </channel>
</rss>

