Step 1: Adding Dimensions (R)

You add dimensions to a pivot table with the Append or Insert method.

Example: Using the Append Method

table = spss.BasePivotTable("Table Title",
                            "OMS table subtype")
coldim=BasePivotTable.Append(table,Dimension.Place.column,"coldim")
rowdim1=BasePivotTable.Append(table,Dimension.Place.row,"rowdim-1")
rowdim2=BasePivotTable.Append(table,Dimension.Place.row,"rowdim-2")
  • The first argument to Append is a reference to the BasePivotTable object--in this example, the R variable table.
  • The second argument to the Append method specifies the type of dimension, using one member from a set of built-in object properties: Dimension.Place.row for a row dimension, Dimension.Place.column for a column dimension, and Dimension.Place.layer for a layer dimension.
  • The third argument to Append is a string that specifies the name used to label this dimension in the displayed table.
  • A reference to each newly created dimension object is stored in a variable. For instance, the variable rowdim1 holds a reference to the object for the row dimension named rowdim-1.
Figure 1. Resulting table structure
Resulting table structure

The order in which the dimensions are appended determines how they are displayed in the table. Each newly appended dimension of a particular type (row, column, or layer) becomes the current innermost dimension in the displayed table. In the example above, rowdim-2 is the innermost row dimension since it is the last one to be appended. Had rowdim-2 been appended first, followed by rowdim-1, rowdim-1 would be the innermost dimension.

Note: Generation of the resulting table requires more code than is shown here.

Example: Using the Insert Method

table = spss.BasePivotTable("Table Title",
                            "OMS table subtype")
rowdim1=BasePivotTable.Append(table,Dimension.Place.row,"rowdim-1")
rowdim2=BasePivotTable.Append(table,Dimension.Place.row,"rowdim-2")
rowdim3=BasePivotTable.Insert(table,2,Dimension.Place.row,"rowdim-3")
coldim=BasePivotTable.Append(table,Dimension.Place.column,"coldim")
  • The first argument to Insert is a reference to the BasePivotTable object--in this example, the R variable table.
  • The second argument to the Insert method specifies the position within the dimensions of that type (row, column, or layer). The first position has index 1 and defines the innermost dimension of that type in the displayed table. Successive integers specify the next innermost dimension and so on. In the current example, rowdim-3 is inserted at position 2 and rowdim-1 is moved from position 2 to position 3.
  • The third argument to Insert specifies the type of dimension, using one member from a set of built-in object properties: Dimension.Place.row for a row dimension, Dimension.Place.column for a column dimension, and Dimension.Place.layer for a layer dimension.
  • The fourth argument to Insert is a string that specifies the name used to label this dimension in the displayed table.
  • A reference to each newly created dimension object is stored in a variable. For instance, the variable rowdim3 holds a reference to the object for the row dimension named rowdim-3.
Figure 2. Resulting table structure
Resulting table structure

Note: Generation of the resulting table requires more code than is shown here.