cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
ashwint27
Level III

Calculate Column Difference for other column levels where lag varies

I have a column :EVENT (character-nominal) with various values.  Also another column :datetime (numeric-continuous).  I have :datetime column sorted ascending.  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.  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.  A solution for datetime sorted-ascending is appreciated, but also not sorted would also be beneficial.

ashwint27_0-1723138090598.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: Calculate Column Difference for other column levels where lag varies

You can for example use variable in the formula to store the value of last disconnect

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

jthi_0-1723139562191.png

 

-Jarmo

View solution in original post

5 REPLIES 5
jthi
Super User

Re: Calculate Column Difference for other column levels where lag varies

You can for example use variable in the formula to store the value of last disconnect

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

jthi_0-1723139562191.png

 

-Jarmo
ashwint27
Level III

Re: Calculate Column Difference for other column levels where lag varies

This approach worked great, thanks!

jthi
Super User

Re: Calculate Column Difference for other column levels where lag varies

You can also use matrices

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 << get values;
times = :T << get values;

discs = Loc(events, "Disc");
cons = Loc(events, "Con");

difs = J(1, N Rows(dt), .);
difs[cons] = times[cons] - times[discs];

dt << new column("difs", numeric, continuous, values(difs));

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

-Jarmo
ashwint27
Level III

Re: Calculate Column Difference for other column levels where lag varies

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.  In this case, I'd like the difference to only be calculated on the first CONNECT Event that appears after a DISCONNECT Event.  Is there a slight modification to the script that can accommodate this?  Thanks again for your help.

txnelson
Super User

Re: Calculate Column Difference for other column levels where lag varies

Here is a modification to @jthi code that should handle this.

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

  I suggest you take the time to read the Scripting Guide, available under JMP Help.  Also please take the time to study the code provided by responding Discussion Forum members.  The modifications to Jarmo's script did not require any new scripting knowledge.  It only required some additional logic added to the flow of the script.  It should be a user's goal to be able to make their own enhancements to the scripts

Jim