Publication date: 07/08/2024

Group Columns

You can group columns by sending the data table the Group Columns message, which takes a list of columns to group as an argument. For example, the following code opens the Big Class.jmp sample data table and groups the age and sex columns.

dt = Open( "$SAMPLE_DATA\Big Class.jmp" );
dt << Group Columns( {:age, :sex} );

"age etc."

You can also send a column name followed by the number of columns to include in the group. The group includes the first column named and the n-1 columns that follow it. This line is equivalent to the line above, which groups age and sex:

dt << Group Columns( :age, 2 );

The group name is based on the first column specified in the argument. So in the preceding example, the group is automatically named age etc. To customize the name, include the group name as the first argument.

dt << Group Columns( "My group", :age, 2 );

To ungroup grouped columns, use the Ungroup Columns message, which takes a list of columns in a group as an argument. For example, the following line ungroups the two columns grouped in the previous example.

dt << Ungroup Columns( {:age, :sex} );

Note the following about grouping and ungrouping columns:

Both messages take a single list as an argument. The list must be enclosed in braces.

You cannot create more than one group in a single message (for example, by giving the Group Columns message two lists of columns). Instead, you must send the data table two separate Group Columns messages.

The Ungroup Columns message takes a list of columns to ungroup, not the name of a group of columns. You can remove a partial list of columns from a group. For example, this line creates a group of four columns:

dt << Group Columns( {:age, :sex, :height, :weight} );

And this line removes two of the columns, while leaving the other two in the group:

dt << Ungroup Columns( {:age, :sex} );

Notice that the column group’s name does not change automatically. You have to change the name.

Get Column Groups or Names

To get column groups or names, use the Get Column Group or Get Column Groups Names messages. The first part of this example creates two sets of grouped columns as shown in the previous section. Get Column Group() returns the column names in the specific group. Get Column Groups Names returns the names of the column groups.

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt << Group Columns( {:age, :sex} );

"age etc."

dt << Group Columns( {:weight, :height} );

"height etc."

dt << Get Column Group( "age etc." );

{:age, :sex}

dt << Get Column Groups Names();

{"age etc.", "height etc."}

Select a Column Group

To select or deselect a column group, use the following messages:

dt << Select Column Group( "name" ); // select the columns in the group
dt << Deselect Column Group( "name" ); // deselect the columns in the group

The following example groups columns X and Y and groups columns Ozone through Lead, and then selects those groups in the data table:

dt = Open( "$SAMPLE_DATA/Cities.jmp" );
dt << Group Columns( "xy", {:X, :Y} );
dt << Group Columns(
	"pollutants",
	:Ozone :: :Lead
);
dt << Select Column Group( "xy", "pollutants" );

Rename a Column Group

To rename a column group, use the following message:

dt << Rename Column Group( "name", "toname" );

The following example renames the column group from xy to coordinates:

dt = Open( "$SAMPLE_DATA/Cities.jmp" );
dt << Group Columns( "xy", {:X, :Y} );
dt << Group Columns(
	"pollutants",
	:Ozone :: :Lead
);
Wait( 3 );
dt << Rename Column Group( "xy", "coordinates" );

Move a Column Group

To move a column group, use the following message:

dt << Move Column Group (To First | To Last | After(column) | After(group));

The following example moves the columns in the pollutants group to the end:

dt = Open( "$SAMPLE_DATA/Cities.jmp" );
dt << Group Columns( "xy", {:X, :Y} );
dt << Group Columns(
	"pollutants",
	:Ozone :: :Lead
);
dt << Move Column Group( To Last, "pollutants" );
Want more information? Have questions? Get answers in the JMP User Community (community.jmp.com).