Choose Language Hide Translation Bar
Highlighted
raj138
Level II

Need to help to use Split function

Can you somebody provide instructions to modify the attached original table to get to the final table. I assume I need to use Split function and then manually move the coloumns around, but the table I have on hand has multiple rows and coloumns so manually moving around is not an option. Any tricks that can be done here?

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
txnelson
Super User

Re: Need to help to use Split function

Oops....my error, here is a corrected script

Names Default To Here( 1 );
dt = Current Data Table();
colNames = dt << get column names( string );
Summarize( dt, slotGroups = by( :Slot ) );

dt << select where( :Slot == slotGroups[1] );
dtTrans = dt << subset( selected rows( 1 ), selected columns( 0 ) );

For( i = 2, i <= N Items( slotGroups ), i++,
	dt << select where( :Slot == slotGroups[i] );
	dtTemp = dt << subset( selected rows( 1 ), selected columns( 0 ) );
	For( k = 1, k <= N Cols( dtTemp ), k++,
		Column( dtTemp, k ) << set name( colNames[k] || " " || Char( i ) )
	);
	dtTrans << Update( With( dtTemp ) );
	Close( dtTemp, nosave );
);
Jim

View solution in original post

4 REPLIES 4
Highlighted
txnelson
Super User

Re: Need to help to use Split function

Here is a simple script that I would use to create the table you want.

Names Default To Here( 1 );
dt = Current Data Table();
colNames = dt << get column names( string );
Summarize( dt, fruitGroups = by( :Fruit ) );

dt << select where( :Fruit == fruitGroups[1] );
dtTrans = dt << subset( selected rows( 1 ), selected columns( 0 ) );

For( i = 2, i <= N Items( fruitGroups ), i++,
	dt << select where( :Fruit == fruitGroups[i] );
	dtTemp = dt << subset( selected rows( 1 ), selected columns( 0 ) );
	For( k = 1, k <= N Cols( dtTemp ), k++,
		Column( dtTemp, k ) << set name( colNames[k] || " " || Char( i ) )
	);
	// The line below was first published in error it has been corrected
	// dtTrans << Update( With( Data Table( "Subset of Original table 2" ) ) );
	dtTrans << Update( With( dtTemp ) );
	Close( dtTemp, nosave );
);
Jim
Highlighted
raj138
Level II

Re: Need to help to use Split function

Hi,

Thank you for the script. While it works amazingly for the table that I provided, I am having difficulty in making it work for my actual data set. Attached is the data set and below is the modified code. Could you please take a look:

 

Names Default To Here( 1 );
dt = Current Data Table();
colNames = dt << get column names( string );
Summarize( dt, slotGroups = by( :Slot ) );

dt << select where( :Slot == slotGroups[1] );
dtTrans = dt << subset( selected rows( 1 ), selected columns( 0 ) );

For( i = 2, i <= N Items( slotGroups ), i++,
	dt << select where( :Slot == slotGroups[i] );
	dtTemp = dt << subset( selected rows( 1 ), selected columns( 0 ) );
	For( k = 1, k <= N Cols( dtTemp ), k++,
		Column( dtTemp, k ) << set name( colNames[k] || " " || Char( i ) )
	);
	dtTrans << Update( With( Data Table( "Subset of Original table 2" ) ) );
	Close( dtTemp, nosave );
);

 

 

 

Highlighted
txnelson
Super User

Re: Need to help to use Split function

Oops....my error, here is a corrected script

Names Default To Here( 1 );
dt = Current Data Table();
colNames = dt << get column names( string );
Summarize( dt, slotGroups = by( :Slot ) );

dt << select where( :Slot == slotGroups[1] );
dtTrans = dt << subset( selected rows( 1 ), selected columns( 0 ) );

For( i = 2, i <= N Items( slotGroups ), i++,
	dt << select where( :Slot == slotGroups[i] );
	dtTemp = dt << subset( selected rows( 1 ), selected columns( 0 ) );
	For( k = 1, k <= N Cols( dtTemp ), k++,
		Column( dtTemp, k ) << set name( colNames[k] || " " || Char( i ) )
	);
	dtTrans << Update( With( dtTemp ) );
	Close( dtTemp, nosave );
);
Jim

View solution in original post

Highlighted
raj138
Level II

Re: Need to help to use Split function

Works like a charm, thank you very much.

Article Labels

    There are no labels assigned to this post.