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
JMP_user1
Level II

How can i subtract row value from previous value and continue til end of table? rows have mx values

Here is the sample. Delta column is what I am trying to acheive. Part# are category.

Part#DataDelta
144 - 4
255 - 5
366 - 6
174 - 7
285 - 8
396 - 9
1107 - 10
2118 - 11
3129 - 12
1 ACCEPTED SOLUTION

Accepted Solutions
JMP_user1
Level II

Re: How can i subtract row value from previous value and continue til end of table? rows have mx val

Do I need to define "Delta Pattern" column?

View solution in original post

6 REPLIES 6
txnelson
Super User

Re: How can i subtract row value from previous value and continue til end of table? rows have mx val

As a column formula

If( Row() <= 3, :Data, :Data - Row() );

In a Script

For Each Row(
     If( Row() <= 3, :Delta = :Data, :Delta = :Data - Row() );
);
Jim
JMP_user1
Level II

Re: How can i subtract row value from previous value and continue til end of table? rows have mx val

Hi Jim,

   thanks for prompt response.

this works in case the row number lines up with values in Data column for subtraction. what if there are random values in Data column and want to consider the raw values of data columns for subtraction?

JMP_user1
Level II

Re: How can i subtract row value from previous value and continue til end of table? rows have mx val

Sample data

Part#DataDelta
100 - 0
200 - 0
300 - 0
190 - 9
2230 - 23
3450 - 45
119 - 1
23423 - 34
35645 - 56
txnelson
Super User

Re: How can i subtract row value from previous value and continue til end of table? rows have mx val

Here is how I would do it

Names Default To Here( 1 );

// Create the sample data table
dt = New Table( "Example",
	Add Rows( 9 ),
	New Column( "Part#", Character, "Nominal", Set Values( {"1", "2", "3", "1", "2", "3", "1", "2", "3"} ) ),
	New Column( "Data",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [0, 0, 0, 9, 23, 45, 1, 34, 56] )
	),
	New Column( "Delta Pattern",
		Character,
		"Nominal",
		Set Values( {"0 - 0", "0 - 0", "0 - 0", "0 - 9", "0 - 23", "0 - 45", "9 - 1", "23 - 34", "45 - 56"} )
	)
);

// Add a new column to provide a path back to the original order
dt << New Column( "RowNum", formula( Row() ) );
// Delete the formula, so the values become static rather than calculated
dt:RowNum << delete formula;

// Sort the data by Part# and Row
dt << sort( by( Part#, :RowNum ), replace table( 1 ) );

// Create the new column with the delta values, which is pretty simple with all of
// the data sorted by Part#
dt << New Column( "Delta", formula( If( Lag( :Part# ) != Part#, 0, Lag( :Data ) - :Data ) ) );
// Delete formula so the values become static
dt:Delta << delete formula;

// Sort back into the original order
dt << sort( by( :RowNum ), replace table( 1 ) );

// Delete the Rownum column since it is no longer needed
dt << delete columns( "RowNum" );

There are otherways to do it too, but they would be slower to calculate

Jim
JMP_user1
Level II

Re: How can i subtract row value from previous value and continue til end of table? rows have mx val

Do I need to define "Delta Pattern" column?

txnelson
Super User

Re: How can i subtract row value from previous value and continue til end of table? rows have mx val

  1. In my example, the column that you called "Delta", I change to be called "Delta Pattern".  I did that because I wanted the the results of the calculation to be placed into a column named "Delta", since the word delta refers to the difference.  So, what you need to do, to match what I did, is to change the name of the column in your starting data table from "Delta" to "Delta Pattern".
  2. You have marked your latest response as a solution.  This is not a solution, but another question.  You need to remove the Solution selection from that item, and place on the item that actually shows the solution.
  3. The assumption that I believe most of the individuals that respond to questions posted in the Community Discussion, is that what is presented back to the questioner, is examined and understood.  Not just copied and pasted into their solution without understanding what was provided for them.  And if in the examination of the response, something is not understood, then a question on that specific area is asked back to the community.  Therefore, the end result is learning, and movement by the questioner, to a point where they can start to contribute as a respondent to other community members.
Jim