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

Subtract between multiple columns

 

dt = current data table();
For ( i=0 , i<8, i++,
dt << 	new column( "RESULT", 
		Numeric,
		"Continuous",
		For (x=1, x<50, x++,
			Formula(Column(3+i)[x]-Column(2+i)[x]),
		));
);

 

I would like to subtract data between multiple columns, i have tried the script above but its not working. Is there a more effective way to do it?

Expected result:

result = data2 - data1

result1 = data4 - data3

result2 = data6 - data5

Itemdata1data2data3data4data5data6data7data8data9data10data11data12data13data14data15data16
A10.510.530.480.530.490.600.560.690.620.770.550.820.580.850.610.88
A20.490.510.480.530.480.600.570.700.610.780.560.830.590.860.640.90
A30.500.520.460.520.490.600.560.690.620.770.540.810.580.840.620.88
A40.470.500.480.530.480.600.570.700.630.780.560.830.600.860.630.89
A50.490.500.510.520.590.600.680.700.740.780.800.840.820.870.850.90
A60.470.500.490.540.480.610.550.700.620.780.590.850.640.890.690.94
A70.480.510.480.530.480.600.580.710.650.800.600.850.630.880.650.90
A80.510.530.480.530.490.600.560.690.620.780.540.820.580.850.630.89
A90.500.520.480.530.480.590.490.690.610.770.550.820.600.860.630.89
A100.510.530.490.540.490.600.540.690.610.770.540.820.570.850.620.89
A110.520.530.520.530.590.600.680.700.740.780.790.830.810.860.840.90
A120.510.520.530.540.610.620.680.700.760.790.800.840.830.880.860.92
A130.480.510.480.530.480.600.570.700.640.790.560.830.590.860.640.90
A140.470.500.470.530.490.600.570.700.630.780.560.830.590.860.630.90
A150.470.500.470.520.470.590.560.690.630.780.560.830.590.860.640.90
A160.510.530.480.530.480.600.570.700.620.770.540.820.580.850.620.89
A170.480.510.460.510.470.590.560.690.620.770.550.820.600.860.630.89
A180.480.510.470.520.480.600.560.700.610.770.560.830.610.870.640.90
A190.520.540.490.540.490.600.570.700.630.780.560.830.600.860.640.90
A200.510.520.520.530.580.600.670.700.750.790.800.840.830.880.860.91
A210.530.530.530.540.600.610.690.710.750.790.800.840.820.870.850.90
A220.530.530.530.540.600.610.680.700.750.790.800.840.820.870.850.91
A230.480.510.480.530.500.610.560.690.620.770.530.820.590.860.620.89
A240.480.510.470.530.480.600.590.720.650.800.600.850.630.880.640.90
A250.480.510.470.520.480.600.570.700.620.770.540.820.590.860.620.89
A260.490.510.470.520.490.600.570.700.640.790.570.840.590.860.640.90
A270.510.520.520.530.590.600.680.700.740.780.790.830.820.860.850.89
A280.510.520.520.530.590.600.680.700.740.780.790.830.810.860.850.90
A290.520.540.490.540.510.610.580.710.650.800.580.840.630.880.680.93
A300.480.510.470.520.390.590.570.700.640.790.580.840.600.860.630.89
A310.490.520.460.520.480.600.570.700.630.780.570.830.610.870.640.90
A320.510.530.480.530.500.610.570.700.630.780.560.830.600.860.630.89
A330.500.520.480.530.500.610.570.700.630.780.560.830.600.870.650.91
A340.480.510.480.530.490.600.580.710.640.790.560.840.590.860.620.89
A350.470.500.470.520.470.590.550.690.610.770.550.820.600.860.650.90
A360.490.520.470.530.480.590.560.690.610.770.550.820.600.860.630.89
A370.480.510.480.530.480.600.580.710.640.790.570.840.610.870.650.91
A380.500.520.480.530.480.600.570.700.630.780.590.850.610.880.650.91
A390.530.530.520.530.590.600.680.700.740.780.790.830.830.870.850.90
A400.530.530.530.540.600.610.680.700.750.790.800.840.830.870.850.91
A410.510.530.480.530.490.600.540.690.610.770.550.820.600.860.640.90
A420.590.500.470.520.490.600.570.700.640.790.560.830.610.870.640.90
A430.510.520.520.530.590.600.680.700.740.780.790.830.820.860.850.90
A440.510.520.520.530.590.600.680.700.740.780.790.830.810.850.840.89
A450.510.520.530.540.590.600.690.710.760.800.810.850.830.880.860.92
A460.490.510.480.530.480.600.570.700.620.780.560.830.610.870.660.91
A470.490.520.480.530.470.590.540.690.610.760.520.810.560.840.610.88
A480.470.500.470.520.470.590.570.700.630.780.560.830.590.860.630.90
A490.500.510.520.530.590.600.680.700.740.780.790.830.820.870.850.90

 

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: Subtract between multiple columns

This might give some additional ideas

Names Default To Here(1);

dt = Open("$DOWNLOADS/724991.jmp");


cont_cols = dt << Get Column Names(Continuous, "String");
first_cols = cont_cols[2::N Items(cont_cols)::2];
second_cols = cont_cols[1::N Items(cont_cols)::2];

If(N Items(first_cols) != N Items(second_cols),
	Throw("column mismatch");
);

For Each({{col1, col2}}, Across(first_cols, second_cols),
	Eval(EvalExpr(
		new_col = dt << New Column("Result", Numeric, Continuous, Formula(
			Expr(Name Expr(AsColumn(dt, col1))) - Expr(Name Expr(AsColumn(dt, col2)))
		));		
	));
	dt << Run Formulas;
	new_col << Suppress Eval(1);
	
);

If you just care about the results, other option is to stack your data, calculate the result to stacked table, split it (might have to add new column for proper result column naming) and join it back

jthi_0-1708582900793.png

jthi_1-1708582924610.png

jthi_3-1708583217261.png

and then JMP is able to create some sort of a script for you

jthi_2-1708583188281.png

 

-Jarmo

View solution in original post

4 REPLIES 4
WebDesignesCrow
Super User

Re: Subtract between multiple columns

Hi @kuanaunwei ,

For me, the effective way to learn the correct script is to use "Workflow" (I'm using JMP 17).

Below is the how I would do it;

I use button-click to get the result manually - then, check the code at "Workflow". Add it in my script

WebDesignesCrow_0-1708576488388.png

From your JSL script above, I'm not sure, whether you just want to have a simple formula column or formula column with conditions (IF/Else)?

 

kuanaunwei
Level III

Re: Subtract between multiple columns

The column names will be different, and the size of the data will also be different. So workflow might not be a best solution for my case. Thanks for the recommendation  

jthi
Super User

Re: Subtract between multiple columns

This might give some additional ideas

Names Default To Here(1);

dt = Open("$DOWNLOADS/724991.jmp");


cont_cols = dt << Get Column Names(Continuous, "String");
first_cols = cont_cols[2::N Items(cont_cols)::2];
second_cols = cont_cols[1::N Items(cont_cols)::2];

If(N Items(first_cols) != N Items(second_cols),
	Throw("column mismatch");
);

For Each({{col1, col2}}, Across(first_cols, second_cols),
	Eval(EvalExpr(
		new_col = dt << New Column("Result", Numeric, Continuous, Formula(
			Expr(Name Expr(AsColumn(dt, col1))) - Expr(Name Expr(AsColumn(dt, col2)))
		));		
	));
	dt << Run Formulas;
	new_col << Suppress Eval(1);
	
);

If you just care about the results, other option is to stack your data, calculate the result to stacked table, split it (might have to add new column for proper result column naming) and join it back

jthi_0-1708582900793.png

jthi_1-1708582924610.png

jthi_3-1708583217261.png

and then JMP is able to create some sort of a script for you

jthi_2-1708583188281.png

 

-Jarmo
kuanaunwei
Level III

Re: Subtract between multiple columns

Sorry for the late response, I could not log into my JMP account for some reason. Also, thanks for the idea given on the scripting side. That helped