Choose Language Hide Translation Bar
Highlighted
lwx228
Level VII

How to achieve automatic batch replacement with JSL?

This table is generated dynamically by the JSL running process. How to use the JSL automatic implementation to replace the 0 value starting from column 2 of this table with a null value "" ?


JSL is required to be replaced with a list of column Numbers or dynamic names.

 

Either a column - by - column replacement or a one-time replacement.

 

2019-09-23_21-43-43.png

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

Re: How to achieve automatic batch replacement with JSL?

I cannot test this script but it should work.

 

Names Default to Here( 1 );

// assume that data table is open
dt = Current Data Table();

For( c = 2, c <= N Cols( dt ), c++,
	target row = dt << Get Rows Where( Column( dt, c )[] == 0 );
	If( N Rows( target row ) > 0,
		For( r = 1, r <= N Rows( target row ), r++,
			Column( dt, c )[target row[r]] = .;
		);
	);
);
Learn it once, use it forever!

View solution in original post

9 REPLIES 9
Highlighted
lwx228
Level VII

Re: How to achieve automatic batch replacement with JSL?

Upload a data file, Ask the experts for help.Thank you very much!

Highlighted
lwx228
Level VII

Re: How to achieve automatic batch replacement with JSL?

I was careless. I used the grammar of VBA directly and forgot to use "Empty()".
Highlighted
MichelleG
Level III

Re: How to achieve automatic batch replacement with JSL?

I don't know if this is the most computationally-efficient way of doing this -- in fact, I assume it's not. But if you need a quick brute-force solution, this has worked for me in the past: loop over the columns, then loop over the rows looking for the value you want to replace, setting the new value as you find it.

 

dt = current data table();
colnames = dt << Get Column Names(String);
for(c=2,c<=N Items(colnames),c++, // starting at 2 per your note
	For Each Row(
		If(column(dt,colnames[c])[]==0, // if you find a 0
			column(dt,colnames[c])[]=Empty() // make it empty
		)
	)
);
Highlighted

Re: How to achieve automatic batch replacement with JSL?

I cannot test this script but it should work.

 

Names Default to Here( 1 );

// assume that data table is open
dt = Current Data Table();

For( c = 2, c <= N Cols( dt ), c++,
	target row = dt << Get Rows Where( Column( dt, c )[] == 0 );
	If( N Rows( target row ) > 0,
		For( r = 1, r <= N Rows( target row ), r++,
			Column( dt, c )[target row[r]] = .;
		);
	);
);
Learn it once, use it forever!

View solution in original post

Highlighted
MichelleG
Level III

Re: How to achieve automatic batch replacement with JSL?

Nice, this cuts down on the number of rows to loop over, saving time on big tables. I was thinking there might be a way to use the Recode functionality on the table as a whole, but I see you've (@markbailey) already explained elsewhere that this is an interactive editor, not a JSL function.

Highlighted

Re: How to achieve automatic batch replacement with JSL?

Such processing will be even easier and faster in JMP 15. You will be able assign the missing value to all the rows in one call.

 

Names Default to Here( 1 );

// assume that data table is open
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
Wait( 1 );

// create 5 observations of zero
n = N Row( dt );
For( i = 1, i < 6, i++,
	r = Random Integer( 1, n );
	:weight[r] = 0;
	r = Random Integer( 1, n );
	:height[r] = 0;
);
Wait( 1 );

For( c = 4, c <= N Cols( dt ), c++,
	target row = dt << Get Rows Where( Column( dt, c )[] == 0 );
	If( N Rows( target row ) > 0,
		Column( dt, c )[target row] = .;
	);
);
Learn it once, use it forever!
Highlighted
lwx228
Level VII

Re: How to achieve automatic batch replacement with JSL?

I'm really looking forward to using JMP 15.Thank you very much!
Highlighted
pmroz
Super User

Re: How to achieve automatic batch replacement with JSL?

The syntax:

For( c = 4, c <= N Cols( dt ), c++,
	target row = dt << Get Rows Where( Column( dt, c )[] == 0 );
	If( N Rows( target row ) > 0,
		Column( dt, c )[target row] = .;
	);
);

already works in JMP 14.

Highlighted
lwx228
Level VII

Re: How to achieve automatic batch replacement with JSL?

  • Thank you very much!

    • I have used the following code before:

    For( i = 3, i <= N Col( d2 ), i++,
    	As Column( i )[d2 << get rows where( As Column( i ) == 0 )] = Empty()
    );
Article Labels

    There are no labels assigned to this post.