BookmarkSubscribeRSS Feed
Choose Language Hide Translation Bar
AT

Community Trekker

Joined:

Dec 2, 2017

Change column data type with special names

I like to write a script to get the column names that starts with "WIFI" and "BR" and then change the modeling type as "Continuous"). Something like belwo

 

Current data Table();

 

col_list = dt << get column names( "WIFI","BR" );

 

col_list << Set Modeling Type("Continuous");

 

Can you help me ? Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
AT

Community Trekker

Joined:

Dec 2, 2017

Solution

Re: Change column data type with special names

Thanks so much. It works fine.

5 REPLIES 5
ih

Community Trekker

Joined:

Sep 30, 2016

Re: Change column data type with special names

Maybe something like this?

Names default to here ( 1 );

dt = Open( "$Sample_data/iris.jmp" );

cols = dt << Get column Names();

for( i = 1, i <= N Items( cols ), i++,
	if( contains( uppercase( cols[i] ), "SEPAL" ) | 
		contains( uppercase( cols[i] ), "WIDTH" )
	,
		Column( dt, cols[i] ) << Set Modeling Type( "Nominal" );
	)
);
txnelson

Super User

Joined:

Jun 22, 2012

Re: Change column data type with special names

I am taking a small amount of liberty with @ih JSL.  Since you specified, that  you are looking for column names that "begin" with specific string values, I would insure this by specifying that the Contains return value has to be a 1, indicating the value was found in column 1.

dt = Open( "$Sample_data/iris.jmp" );

cols = dt << Get column Names();

for( i = 1, i <= N Items( cols ), i++,
	if( contains( uppercase( cols[i] ), "SEPAL" ) ==1  | 
		contains( uppercase( cols[i] ), "WIDTH" ) == 1
	,
		Column( dt, cols[i] ) << Set Modeling Type( "Nominal" );
	)
);

You may also choose the following methodology, which I find to actually be more readable

Names Default to Here( 1 );
dt = Open( "$Sample_data/iris.jmp" );

cols = dt << Get column Names();

for( i = 1, i <= N Items( cols ), i++,
	if( Left( uppercase( cols[i] ), 5 ) == "SEPAL"  | 
		Left( uppercase( cols[i] ), 5 ) == "WIDTH" 
	,
		Column( dt, cols[i] ) << Set Modeling Type( "Nominal" );
	)
);
Jim
ih

Community Trekker

Joined:

Sep 30, 2016

Re: Change column data type with special names

Good catch @txnelson.  Given any more than a simple match I default to regex.  Thus I present yet another option:

dt = Open( "$Sample_data/iris.jmp" );

cols = dt << Get column Names();

for( i = 1, i <= N Items( cols ), i++,
	if( !Is missing(Regex( char( cols[i] ) , "^(sepal|width)(.*)", "\0", IGNORECASE) ),
		Column( dt, cols[i] ) << Set Modeling Type( "Nominal" );
	)
);
AT

Community Trekker

Joined:

Dec 2, 2017

Solution

Re: Change column data type with special names

Thanks so much. It works fine.

txnelson

Super User

Joined:

Jun 22, 2012

Re: Change column data type with special names

I don't think the response you selected as "The Solution" is the solution.  

Jim