cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
VD
VD
Level I

Please help for how to script to select the 4 highest cell value in a column

1 ACCEPTED SOLUTION

Accepted Solutions
Craige_Hales
Super User

Re: Please help for how to script to select the 4 highest cell value in a column

dt = Open( "$sample_data/big class.jmp" );
nr = N Rows( dt ); // needed below to find the end of the ascending sort list
rowsSortedByWeight = Rank( dt:weight << getvalues ); // Rank gives the indexes, ascending
heavyRows = rowsSortedByWeight[nr - 3 :: nr]; // four biggest at end
For Each( {r}, heavyRows, // get each row number into r from the heavyRows matrix
	// look in the log for the report...
	Write( Eval Insert( "\!nname=^dt:name[r]^ weight=^dt:weight[r]^" ) ) 
);

name=KIRK weight=134
name=LESLIE weight=142
name=JACLYN weight=145
name=LAWRENCE weight=172

edit: this will work better with missing values:

dt = Open( "$sample_data/big class.jmp" );
rowsSortedByWeight = Rank( dt:weight << getvalues ); // Rank gives the indexes, ascending
nr = N Rows( rowsSortedByWeight ); // should handle missing values
heavyRows = rowsSortedByWeight[nr - 3 :: nr]; // four biggest at end
For Each( {r}, heavyRows, // get each row number into r from the heavyRows matrix
	// look in the log for the report...
	Write( Eval Insert( "\!nname=^dt:name[r]^ weight=^dt:weight[r]^" ) ) 
);

Rank() will return a short list, without indexes to missing values, so use the number of rows in the ranked list rather than the number of rows in the data table.

more edit: And if you mean to select the rows in the table,

dt << selectrows( heavyRows )
Craige

View solution in original post

2 REPLIES 2
Craige_Hales
Super User

Re: Please help for how to script to select the 4 highest cell value in a column

dt = Open( "$sample_data/big class.jmp" );
nr = N Rows( dt ); // needed below to find the end of the ascending sort list
rowsSortedByWeight = Rank( dt:weight << getvalues ); // Rank gives the indexes, ascending
heavyRows = rowsSortedByWeight[nr - 3 :: nr]; // four biggest at end
For Each( {r}, heavyRows, // get each row number into r from the heavyRows matrix
	// look in the log for the report...
	Write( Eval Insert( "\!nname=^dt:name[r]^ weight=^dt:weight[r]^" ) ) 
);

name=KIRK weight=134
name=LESLIE weight=142
name=JACLYN weight=145
name=LAWRENCE weight=172

edit: this will work better with missing values:

dt = Open( "$sample_data/big class.jmp" );
rowsSortedByWeight = Rank( dt:weight << getvalues ); // Rank gives the indexes, ascending
nr = N Rows( rowsSortedByWeight ); // should handle missing values
heavyRows = rowsSortedByWeight[nr - 3 :: nr]; // four biggest at end
For Each( {r}, heavyRows, // get each row number into r from the heavyRows matrix
	// look in the log for the report...
	Write( Eval Insert( "\!nname=^dt:name[r]^ weight=^dt:weight[r]^" ) ) 
);

Rank() will return a short list, without indexes to missing values, so use the number of rows in the ranked list rather than the number of rows in the data table.

more edit: And if you mean to select the rows in the table,

dt << selectrows( heavyRows )
Craige
VD
VD
Level I

Re: Please help for how to script to select the 4 highest cell value in a column

Thanks much to Craige_Hales for a quick response.

It works well