Does anyone have a script to remove columns that only contain missing values? I am stumbling my way through cleaning up a data table and this would be very helpful in automating this process.
Thanks,
Pete
Pete, if you want to delete the columns (subject of post says replace missings with zeros) this will do it.
dt = current data table(); For( i = N Col( dt ), i >= 1, i--, If( Max( Is Missing( dt[0, i] ) ) > 0, dt << Delete Columns( i ) ) );
Thanks Mike,
Do you know of a way to script this? I had come down to a similar solution, but could not find a way to have this fully contained in a script.
Thanks Brady
@brady_brady's is probably better but I came up with using the All() and Loc() functions for Numeric and Character columns.
Something like this:
dt=New Table( "untitled 25 2", Add Rows( 4 ), New Column( "Column 1", Character, "Nominal", Set Values( {"a", "a", "a", "a"} ) ), New Column( "Column 2", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [., ., ., .] ) ), New Column( "Column 3", Character, "Nominal", Set Values( {"", "", "", ""} ) ), New Column( "Column 4", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [., 0, 0, 0] ) ) ); For( i = N Cols( dt ), i > 0, i--, vals = Column( dt, i ) << get values; show(vals); If( (Column( dt, i ) << get data type) == "Character", Show( Loc( vals, "" ) ), Show( all( vals ) ) ); );
Hey Pete, I thought of this later... it is much faster than the first method I proposed. For example, on a 1000 row x1000 column table, the first method took 0.82 seconds, while this method took 0.017 seconds.
dt << Delete Columns( Loc( V Max( Is Missing( dt << Get All Columns As Matrix ) ) ) );
I spoke (typed) too soon. Once the matrix (datatable) gets a bit larger, the first method is faster. On a 100,000 row x 1000 col matrix, the first method takes 2.48 seconds while the second takes 6.51 seconds. My guess is that for truly large tables, the first method will be merely slow, while the second method will be excrutiatingly so.
Cheers,
Brady