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
kh
kh
Level II

How to make a general / variable script for splitting a data table and performing calculations?

I routinely perform the same data table manipulation and calculations and am interested in automating that with a script.

The general steps are something like:

 

sample split.jpg

  1. Split table (split column "value" by column "attribute") > generates a new table
  2. Generate a new column (C) = column A + column B
  3. Generate new column (D) = column A / column C

 

split.jpg

 

However, every table has a different number of rows and different values.  When I click through these functions and examine the script generated by JMP, the number of rows and values in each column are hard-coded.  How do I make these completely variable so I can run the script on new data tables of any size?

So far, I have a script that looks like this, but it prompts you for the number of rows, then just generates an empty table.  I'm having a hard time finding documentation, so any help is appreciated!

New Table( "split table",
	Add Rows(),
	New Script(
		"Source",
		Data Table() <<
		Split(
			Split By( :attribute ),
			Split( :value ),
			Output Table( "split table" ),
			Sort by Column Property	) ),
	New Column( "date", Character( 1 ), "Nominal", Set Values( {""} ) ),
	New Column( "A", Numeric, "Continuous", Format( "Best", 12 ), Set Values( {""} ) ),
	New Column( "B", Numeric, "Continuous", Format( "Best", 12 ), Set Values( {""} ) ),
	New Column( "C", Numeric, "Continuous", Format( "Best", 12 ), Formula( :Name( "A" ) + :Name( "B" ) ) ),
	New Column( "D", Numeric, "Continuous",	Format( "Percent", 12, 0 ), Formula( :Name( "A" ) / :Name( "C" ) ) ) )

 

1 ACCEPTED SOLUTION

Accepted Solutions
kh
kh
Level II

Re: How to make a general / variable script for splitting a data table and performing calculations?

Hi Jim,

Sorry about the data table.  It seems to work fine on my computer, so I've simply reuploaded it.

I tried your script and it works except for one thing: creating the columns "date", "A" and "B" are redundant, because those columns get carried over to the new table by the split table function.  So the script becomes even simpler and works perfectly:

Names Default To Here( 1 );

// Create a pointer variable to be able to 
// reference the current active data table
dt = Current Data Table();

// Create the split data table
dt2 = dt << Split(
	Split By( :attribute ),
	Split( :value ),
	Output Table( "split table" ),
	Sort by Column Property
);

// Add new columns to the split data table
dt2 << New Column( "C",
	Numeric,
	"Continuous",
	Format( "Best", 12 ),
	Formula( :Name( "A" ) + :Name( "B" ) )
);
dt2 << New Column( "D",
	Numeric,
	"Continuous",
	Format( "Percent", 12, 0 ),
	Formula( :Name( "A" ) / :Name( "C" ) )
);

Thank you so much!

View solution in original post

2 REPLIES 2
txnelson
Super User

Re: How to make a general / variable script for splitting a data table and performing calculations?

1. Your attached data table is corrupted.  I opened it using a text editor, and it has messages of having deleted the data.

2.  From the text of your question, I think I understand how you are thinking your script structure should be doing what you want, but in fact, it will just generate an empty data table....as you have reported it does.

3.  Here is what I think you want as the starting for your script

Names Default To Here( 1 );

// Create a pointer variable to be able to reference 
// the current active data table
dt = Current Data Table();

// Creat the split data table
dt2 = dt << Split(
	Split By( :attribute ),
	Split( :value ),
	Output Table( "split table" ),
	Sort by Column Property
);

// Add the new columns to the split data table
dt2 << New Column( "date", Character( 1 ), "Nominal", Set Values( {""} ) );
dt2 << New Column( "A", Numeric, "Continuous", Format( "Best", 12 ), Set Values( {""} ) );
dt2 << New Column( "B", Numeric, "Continuous", Format( "Best", 12 ), Set Values( {""} ) );
dt2 << New Column( "C",
	Numeric,
	"Continuous",
	Format( "Best", 12 ),
	Formula( :Name( "A" ) + :Name( "B" ) )
);
dt2 << New Column( "D",
	Numeric,
	"Continuous",
	Format( "Percent", 12, 0 ),
	Formula( :Name( "A" ) / :Name( "C" ) )
);

 

Jim
kh
kh
Level II

Re: How to make a general / variable script for splitting a data table and performing calculations?

Hi Jim,

Sorry about the data table.  It seems to work fine on my computer, so I've simply reuploaded it.

I tried your script and it works except for one thing: creating the columns "date", "A" and "B" are redundant, because those columns get carried over to the new table by the split table function.  So the script becomes even simpler and works perfectly:

Names Default To Here( 1 );

// Create a pointer variable to be able to 
// reference the current active data table
dt = Current Data Table();

// Create the split data table
dt2 = dt << Split(
	Split By( :attribute ),
	Split( :value ),
	Output Table( "split table" ),
	Sort by Column Property
);

// Add new columns to the split data table
dt2 << New Column( "C",
	Numeric,
	"Continuous",
	Format( "Best", 12 ),
	Formula( :Name( "A" ) + :Name( "B" ) )
);
dt2 << New Column( "D",
	Numeric,
	"Continuous",
	Format( "Percent", 12, 0 ),
	Formula( :Name( "A" ) / :Name( "C" ) )
);

Thank you so much!