cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Submit your abstract to the call for content for Discovery Summit Americas by April 23. Selected abstracts will be presented at Discovery Summit, Oct. 21- 24.
Discovery is online this week, April 16 and 18. Join us for these exciting interactive sessions.
Choose Language Hide Translation Bar
ILoveJMP
Level III

Max value of each row across multiple columns and the corresponding name of the source column

Hi, is there an easy way to find the max value of each row and the column name (or parameter) corresponding to the max value? Below is a simple example to illustrate the request:

 

T1T2T3T4T5T6T7T8T9T10Max_ValueCol_Name (Parameter)
87.283.681.48383838382.383.384.687.2T1
85.984828483.383.3848386.587.287.2T10
84.982.7868382.382.3838283.685.286T3
85.684.682.784.684.384.384.383.684.986.286.2T10
85.283.68283.3838383.382.384.385.685.6T10
86.884.382.784.383.683.684.383.385.986.286.8T1
86.28482.3848783.383.68384.985.987T5
88.184.682.384.383.68484.383.385.986.288.1T1
85.683.381.783.3838383.682.78485.985.9T10
37.436.136.436.436.136.136.435.436.737.437.4T1
36.734.534.834.533.834.134.533.536.136.136.7T1
34.832.933.232.932.232.53631.933.835.136T7
3836.436.436.436.436.136.7393838.339T8
3835.135.14034.134.534.833.836.43740T4

 

Thanks!

21 REPLIES 21
ravi_sasjmp
Level II

Re: Max value of each row across multiple columns and the corresponding name of the source column

Thanks Craige!

Instead of Concat(), is there any other function that can be used with Substitute Into(), to find position of particular value in a row across multiple columns?

 

 

dt = New Table( "Untitled",
    Add Rows( 3 ),
    New Column( "first", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 5, 9] ) ),
    New Column( "second", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [2, 6, 10] ) ),
    New Column( "skip", character, setvalues( {"a", "b", "c"} ) ),
    New Column( "third", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [3, 7, 11] ) ),
    New Column( "fourth", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [4, 8, 12] ) )
);
names = dt << getcolumnnames( "numeric" ); // returns a list of all numeric column names
Show( names ); // {first, second, third, fourth}
Substitute Into( names, Expr( {} ), Expr( Concat() ) );
Show( Name Expr( names ) ); // first || second || third || fourth
Row() = 2; // names will pull data from the current row
rowpos = Loc( names, 6 ); // works only with a list

 

 

Thanks,

Ravi

Craige_Hales
Super User

Re: Max value of each row across multiple columns and the corresponding name of the source column

I mis-read your question at first...so this answer contains a simplification, not using substituteInto, and a variation that looks for a particular value.

dt = New Table( "Untitled",
 Add Rows( 3 ),
 New Column( "first", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 5, 9] ) ),
 New Column( "second", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [2, 6, 10] ) ),
 New Column( "skip", character, setvalues( {"a", "b", "c"} ) ),
 New Column( "third", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [3, 7, 11] ) ),
 New Column( "fourth", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [4, 8, 12] ) )
);

names = dt << getcolumnnames( "numeric" ); // returns a list of all numeric column names

findval=7;
row = 2;
Row() = row; // names will pull data from the current row
rowpos = Loc( findval==matrix(evallist(names)) ); 

show(names[rowpos],names[rowpos[1]][row]);
names[rowpos] = {:third};
names[rowpos[1]][2] = 7;

Loc returns the positions of the non-zero items in a matrix. The == operator with a scalar and a matrix returns a matrix of 1s and 0s. The Loc function returns a matrix in rowpos (with the indexes of the 1s); the [1] takes the first location that matched findval. names[rowpos[1]] is then a column name, and the [row] gets the matching answer from the column's row.

 

Craige