cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
brandon_mcrv
Level II

Select and delete columns that contain specific string

Hi jsl folks,

 

         Can somebody comment on what's wrong with my coding below? I am trying to look for columns which contain a string called "Dummy" and trying to delete it but it is not working. Pls help.

 

 

Names Default To Here( 1 );

dt = New Table( "Example",
    Add Rows( 3 ),
    New Column( "A_Dummy", Character, "Nominal", Set Values( {"xx", "yy", "zz"} ) ),
    New Column( "B", Character, "Nominal", Set Values( {"a", "b", "c"} ) ),
    New Column( "C_Dummy", Character, "Nominal", Set Values( {"l", "m", "n"} ) )
);

cols = dt << get column names( string );

If( Contains( cols, "Dummy" ), 
    dt << Select Columns();
    dt << Delete Columns();
);

 

 

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
Georg
Level VII

Re: Select and delete columns that contain specific string

This is a modified version that may work for you and that is easy to understand.

You would need a loop over the columns to see, where the character string is in,

and your usage of select and delete was not right.

 

 

Names Default To Here( 1 );

dt = New Table( "Example",
	Add Rows( 3 ),
	New Column( "A_Dummy", Character, "Nominal", Set Values( {"xx", "yy", "zz"} ) ),
	New Column( "B", Character, "Nominal", Set Values( {"a", "b", "c"} ) ),
	New Column( "C_Dummy", Character, "Nominal", Set Values( {"l", "m", "n"} ) )
);

cols = dt << get column names( string );

delete_lst = {};

For( i = 1, i <= N Items( cols ), i++,
	If( Contains( cols[i], "Dummy" ),
		Insert Into( delete_lst, cols[i] )
	)
);

dt << Delete Columns( delete_lst );
Georg

View solution in original post

pauldeen
Level VI

Re: Select and delete columns that contain specific string

Slight modification to eliminate the need for a extra list variable:

Names Default To Here( 1 );

dt = New Table( "Example",
	Add Rows( 3 ),
	New Column( "A_Dummy", Character, "Nominal", Set Values( {"xx", "yy", "zz"} ) ),
	New Column( "B", Character, "Nominal", Set Values( {"a", "b", "c"} ) ),
	New Column( "C_Dummy", Character, "Nominal", Set Values( {"l", "m", "n"} ) )
);

cols = dt << get column names( string );
For( i = N Items( cols ), i>=1, i--,
	If( Contains( cols[i], "Dummy" ),
		dt << Delete Columns( i );
	)
);

View solution in original post

7 REPLIES 7
brandon_mcrv
Level II

Re: Select and delete columns that contain specific string

Just to add on...The reason I am asking this is because I'd like to know if there's a bunch of column names like for example 50 columns which contains "_Dummy", how can I delete it switftly without having to name the specific column names by using known delete columns command like "dt << delete column(column("A_Dummy"))". If I have to use this command, then I'd have to type 50 times all the unique column names.

jthi
Super User

Re: Select and delete columns that contain specific string

Contains will only give you first index of a match. You could either loop over the list to generate list with columns to delete or delete while you loop:

Names Default To Here( 1 );

dt = New Table( "Example",
Add Rows( 3 ),
New Column( "A_Dummy", Character, "Nominal", Set Values( {"xx", "yy", "zz"} ) ),
New Column( "B", Character, "Nominal", Set Values( {"a", "b", "c"} ) ),
New Column( "C_Dummy", Character, "Nominal", Set Values( {"l", "m", "n"} ) ));
cols = dt << get column names( string ); for(i = 1, i <= N Items(cols), i++, If(Contains(cols[i],"Dummy"), dt << Delete Columns(cols[i]) ); );
-Jarmo
brandon_mcrv
Level II

Re: Select and delete columns that contain specific string

Hi jthi, thanks for trying to help. I found that the script actually deletes all the columns regardless of having the "Dummy" string or not. Anyway, Georg and pauldeen already provided the solution.
Georg
Level VII

Re: Select and delete columns that contain specific string

This is a modified version that may work for you and that is easy to understand.

You would need a loop over the columns to see, where the character string is in,

and your usage of select and delete was not right.

 

 

Names Default To Here( 1 );

dt = New Table( "Example",
	Add Rows( 3 ),
	New Column( "A_Dummy", Character, "Nominal", Set Values( {"xx", "yy", "zz"} ) ),
	New Column( "B", Character, "Nominal", Set Values( {"a", "b", "c"} ) ),
	New Column( "C_Dummy", Character, "Nominal", Set Values( {"l", "m", "n"} ) )
);

cols = dt << get column names( string );

delete_lst = {};

For( i = 1, i <= N Items( cols ), i++,
	If( Contains( cols[i], "Dummy" ),
		Insert Into( delete_lst, cols[i] )
	)
);

dt << Delete Columns( delete_lst );
Georg
pauldeen
Level VI

Re: Select and delete columns that contain specific string

Slight modification to eliminate the need for a extra list variable:

Names Default To Here( 1 );

dt = New Table( "Example",
	Add Rows( 3 ),
	New Column( "A_Dummy", Character, "Nominal", Set Values( {"xx", "yy", "zz"} ) ),
	New Column( "B", Character, "Nominal", Set Values( {"a", "b", "c"} ) ),
	New Column( "C_Dummy", Character, "Nominal", Set Values( {"l", "m", "n"} ) )
);

cols = dt << get column names( string );
For( i = N Items( cols ), i>=1, i--,
	If( Contains( cols[i], "Dummy" ),
		dt << Delete Columns( i );
	)
);

brandon_mcrv
Level II

Re: Select and delete columns that contain specific string

Excellent, it works perfectly ! Thanks !
brandon_mcrv
Level II

Re: Select and delete columns that contain specific string

It works ! Thanks for the solution, Georg !