cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
twillkickers
Level III

Apply Mathematical formula only on Selected Rows (JSL)

How do I apply a mathmatical formula to selected rows only using JSL? Thanks for your help.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Apply Mathematical formula only on Selected Rows (JSL)

Here is an example of my interpretation of what you want to do.

Capture.PNG

 

The last column uses a column formula. The formula implements the conditional computation.

Capture.PNG

 

Row states are a vital part of how JMP operates. This code is equivalent but without installing a column formula.

Names Default To Here( 1 );

col = Current Data Table() << New Column( "Conditional BMI" );

For Each Row( col[] = If( Selected( Row State( Row() ) ), (703 * :weight) / :height ^ 2 ) );

View solution in original post

5 REPLIES 5
gzmorgan0
Super User (Alumni)

Re: Apply Mathematical formula only on Selected Rows (JSL)

You did not mention if you just want the value or you wanted this to be another column formula. If it is s column formula you need to provide more information.  Here is an exampe with a computation using only 2 of 6 rows.

 

Names Default to Here(1);

dt = new Table("Test", Add Rows(6), New Column("Values", numeric, continuous, Set Values(J(6,1, Random Integer(1,20))) ) );

mysum = :Values[2]^2 + :Values[4]^2 - 4 * :Values[2]*:Values[4] ;

show(mysum );

 

txnelson
Super User

Re: Apply Mathematical formula only on Selected Rows (JSL)

Here is an example that shows how to deal exclusively with selected rows

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/big class.jmp" );

// pause to show the original data table
wait(5);

// select some rows
dt << select where( :sex == "F" );

For( i = 1, i <= N Rows( dt << get selected rows ), i++,
	dt:age[i] = dt:age[i] - 2
);

dt:weight[dt<<get selected rows] = 100;
Jim

Re: Apply Mathematical formula only on Selected Rows (JSL)

Here is an example of my interpretation of what you want to do.

Capture.PNG

 

The last column uses a column formula. The formula implements the conditional computation.

Capture.PNG

 

Row states are a vital part of how JMP operates. This code is equivalent but without installing a column formula.

Names Default To Here( 1 );

col = Current Data Table() << New Column( "Conditional BMI" );

For Each Row( col[] = If( Selected( Row State( Row() ) ), (703 * :weight) / :height ^ 2 ) );
MROW
Level I

Re: Apply Mathematical formula only on Selected Rows (JSL)

I have similar situation with a data table. My goal is to convert all of the values in the "Values" column from Fahrenheit to Celsius. The script I am using to do this is:

 

For Each Row(If(:Unit=="°F", Selected(RowState())=1));
For Each Row(:Value = If(Selected(RowState(Row())), Conversions:Farenheit To Celsius(:Value)));

 

The script does what I need it to do in terms of the conversion however, all of the values that are not associated with the unit °F are removed. Is there a script that can be added to ignore the unselected columns and ultimately keep the values that are not associated with the unit °F?

 

MROW_0-1643048379719.png

 

jthi
Super User

Re: Apply Mathematical formula only on Selected Rows (JSL)

I wouldn't really suggest directly converting the value of Value field because unless you also change the Unit column to Celsius.

You can use set each value with if-statement to replace values in column. In else part "return" same value there is currently. Something like this should work:

:Value << Set Each Value(
	If(:Unit=="°F" /*& Selected()*/,
		Conversions:Farenheit To Celsius(:Value)
	, //else keep value
		:Value 
	);
)

 

-Jarmo