Scripting Guide > Data Tables > Columns > Create Columns
发布日期: 04/13/2021

Create Columns

To add a new column to a data table, send a New Column message to a data table reference or use the New Column() function. The first argument, the column’s name, is required. Either enclose the name in quotation marks or specify an expression evaluating to the name.

dt = New Table( "MyData1.jmp" );
dt << New Column( "wafer" );

or

dt = New Table( "MyData2.jmp" );
a = "wafer";
dt << New Column( a );

or

dt = New Table( "MyData3.jmp" );
col = New Column( "X", Formula( Random Uniform() ) );

If the table already includes a column by the same name, a sequential numeric value is appended to the new column name (wafer, wafer 2, wafer 3, and so on).

Unless otherwise specified, columns are numeric, continuous, and 12 characters wide.

Data type (numeric, character, row state, or expression)

Modeling type (continuous, nominal, ordinal, multiple response, unstructured text, or vector)

Column width (only for numeric columns)

Numeric format

The following example creates a new column whose data type is numeric, modeling type is continuous, width is set to 5, and numeric format is set to Best:

dt << New Column( "wafer", Numeric, "Continuous", Format( "Best", 5 ) );

The following example creates a character column and automatically assigns the nominal modeling type.

dt << New Column( "Last Name", Character );

You can also add formulas and other script messages appropriate to the column.

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt << New Column( "Ratio", Numeric, "Continuous", Formula( :height/:weight ) );
dt << New Column( "myMarkers",
	Row State,
	Set Formula( Marker State( age - 12 ) )
);

If you plan to work with specific columns later (for example, grouping or changing data types), create a column reference, as follows:

myCol = dt << New Column( "Birth Date" );

Fill a Column with Data

To fill the column with data, use Values or its equivalent, Set Values. Include the value for each cell in a list.

The following example adds a new column called Last Name to a new data table. Three values are added: Smith, Jones, and Anderson.

dt = New Table( "My Data");
dt << New Column( "Last Name", Character, Values( {"Smith", "Jones", "Anderson"} ) );

The column can also be filled with numeric values. The following example adds a new column called Row Number to a new data table. The function N Row returns the number of rows in the table, and numeric values populate all of the rows in the column, beginning with 1.

dt = New Table( "My Data");
dt << New Column( "Last Name", Character, Values( {"Smith", "Jones", "Anderson"} ) );
dt << New Column( "Row Number",
	Numeric,
	Values( 1 :: N Row() ),
	Format( "Best", 5 )
);

To add a column of random numbers, insert a formula along with the random function.

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt << New Column( "Random", Numeric, Formula( Random Uniform() ) );

Another option is to add a constant numeric value to each cell. In the following example, the number 5 is added to each cell.

dt << New Column( "Number" );
:Number << Set Each Value( 5 );

New Column() can also be used as a built-in function, in other words, without the << (Send) command applied to the data table reference. When used in this way, the column is added to the current data table.

dt << New Column( "Address" ); // command is sent to the data table reference
New Column( "Address" ); // column is added to the current data table
需要更多信息?有问题?从 JMP 用户社区得到解答 (community.jmp.com).