Subtotals and headings for Categorical Variables and Multiple Response Variables. These are especially useful for making aggregates across multiple categories (sometimes referred to as nets, top box, or top 2 box).

Subtotals and headings for Categorical Variables and Multiple Response Variables. These are especially useful for making aggregates across multiple categories (sometimes referred to as nets, top box, or top 2 box).

Subtotal(
  name,
  categories = NULL,
  position = c("relative", "top", "bottom"),
  after = NULL,
  before = NULL,
  negative = NULL,
  na.rm = TRUE,
  variable = NULL,
  id = NULL,
  alias = NULL
)

Heading(name, position = c("relative", "top", "bottom"), after = NULL)

subtotals(x)

subtotals(x) <- value

Subtotal(
  name,
  categories = NULL,
  position = c("relative", "top", "bottom"),
  after = NULL,
  before = NULL,
  negative = NULL,
  na.rm = TRUE,
  variable = NULL,
  id = NULL,
  alias = NULL
)

is.Subtotal(x)

is.Heading(x)

are.Subtotals(x)

are.Headings(x)

Heading(name, position = c("relative", "top", "bottom"), after = NULL)

# S4 method for CrunchVariable
subtotals(x)

# S4 method for VariableTuple
subtotals(x)

# S4 method for CrunchVariable,ANY
subtotals(x) <- value

# S4 method for CrunchVariable,`NULL`
subtotals(x) <- value

Subtotal(
  name,
  categories = NULL,
  position = c("relative", "top", "bottom"),
  after = NULL,
  before = NULL,
  negative = NULL,
  na.rm = TRUE,
  variable = NULL,
  id = NULL,
  alias = NULL
)

is.Subtotal(x)

is.Heading(x)

are.Subtotals(x)

are.Headings(x)

Heading(name, position = c("relative", "top", "bottom"), after = NULL)

# S4 method for CrunchVariable
subtotals(x)

# S4 method for VariableTuple
subtotals(x)

# S4 method for CrunchVariable,ANY
subtotals(x) <- value

# S4 method for CrunchVariable,`NULL`
subtotals(x) <- value

Arguments

name

character the name of the subtotal or heading

categories

character or numeric the category names or ids for subtotal only

position

character one of "relative", "top", or "bottom". Determines the position of the subtotal or heading, either at the top, bottom, or relative to another category in the cube (default).

after

character or numeric if position is "relative", then the category name or id to position the subtotal or heading after. If not supplied this defaults to the last of the categories supplied to Subtotal.

before

character or numeric if position is "relative" and also the subtotal is on a Multiple Response variable only.

negative

character or numeric of the category names or ids to be subtracted for subtotals only

na.rm

For Multiple Response subtotals, whether to remove missings before calculating the subtotal (so that if na.rm=TRUE and there is one missing and one selected, the response would selected instead of missing). This defaults to TRUE to match how the crunch web application behaves.

variable

For Multiple Response subtotals, the parent MR variable that contains the subvariables that are being subtotaled (defaults to the variable that is having the Subtotal added to it)

id

For Multiple Response subtotals, an optional number or string to use as the new insertion's id (defaults to a sequential number).

alias

For Multiple Response subtotals, an optional string to use as the new insertion's alias (defaults to letting the server choose the alias)

x

either a variable or CrunchCube object to add or get subtotal transforms for, for is.Subtotal() and is.Heading() an object to test if it is either a Subtotal or Heading

value

For [<-, the replacement Subtotal to insert

Details

To see the subtotals or headings set for a variable, use subtotals(variable)

To see the subtotals or headings set for a variable, use subtotals(variable)

Adding Subtotals and Headings

Subtotals and headings can be added either by passing a list of Subtotals or Headings, or they can be added one at a time by passing Subtotal or Heading to subtotals(variable) alone.

Adding subtotals or headings is additive; meaning that subtotals or headings that are already set on the variable are not removed when new subtotals or headings are added. To remove all subtotals and headings, set subtotals(variable) to NULL.

To get an array of just the subtotal rows from a CrunchCube, use the function subtotalArray(CrunchCube).

Subtotals and headings can be added either by passing a list of Subtotals or Headings, or they can be added one at a time by passing Subtotal or Heading to subtotals(variable) alone.

Adding subtotals or headings is additive; meaning that subtotals or headings that are already set on the variable are not removed when new subtotals or headings are added. To remove all subtotals and headings, set subtotals(variable) to NULL.

To get an array of just the subtotal rows from a CrunchCube, use the function subtotalArray(CrunchCube).

Working with Subtotals and headings

When interacting programmatically with Subtotals and Headings, it can be useful to be able to tell if something is a Subtotal or a Heading. The is.* family of methods are useful here: the singular versions (is.Subtotal and is.Heading) take a single object and returns TRUE if the object is either a Subtotal or a Heading and FALSE if not; the plural versions (are.Subtotals and are.Headings) take a list of objects (including an Insertions object) and returns a vector of TRUE/FALSEs.

When interacting programmatically with Subtotals and Headings, it can be useful to be able to tell if something is a Subtotal or a Heading. The is.* family of methods are useful here: the singular versions (is.Subtotal and is.Heading) take a single object and returns TRUE if the object is either a Subtotal or a Heading and FALSE if not; the plural versions (are.Subtotals and are.Headings) take a list of objects (including an Insertions object) and returns a vector of TRUE/FALSEs.

Removing transforms

noTransforms() is useful if you don't want to see or use any transformations like Subtotals and Headings. This action only applies to the CrunchCube object in R: it doesn't actually change the variables on Crunch servers or the query that generated the CrunchCube.

noTransforms() is useful if you don't want to see or use any transformations like Subtotals and Headings. This action only applies to the CrunchCube object in R: it doesn't actually change the variables on Crunch servers or the query that generated the CrunchCube.

Examples

if (FALSE) {
# given a variable ds$opinion, with categories: Strongly Agree, Somewhat
# Agree, Neither Agree nor Disagree, Somewhat Disagree, and Strongly Disagree,
# to make two subtotals for Agree and Disagree:
subtotals(ds$opinion) <- list(
    Subtotal(
        name = "Agree",
        categories = c("Strongly Agree", "Somewhat Agree"),
        after = "Somewhat Agree"
    ),
    Subtotal(
        name = "Disagree",
        categories = c("Strongly Disagree", "Somewhat Disagree"),
        after = "Strongly Disagree"
    )
)

# headings can also be added:
subtotals(ds$opinion) <- Heading(name = "All opinions", position = "top")

# to see the subtotals and headings associated with a variable
subtotals(ds$opinion)
#        anchor         name     func    args
# 1      2        Agree subtotal 1 and 2
# 2      4     Disagree subtotal 4 and 5
# 3      0 All opinions     <NA>      NA

# when you use a variable with subtotals and headings in a cube, you see them
# by default
opinion_cube <- crtabs(~opinion, ds)
opinion_cube
#               All opinions
#             Strongly Agree 23
#             Somewhat Agree 24
#                      Agree 47
# Neither Agree nor Disagree 18
#          Somewhat Disagree 16
#          Strongly Disagree 19
#                   Disagree 35


# to get just the subtotals,
subtotalArray(opinion_cube)
#    Agree Disagree
#       47       35

# to remove all subtotals and headings
subtotals(ds$opinion) <- NULL
crtabs(~opinion, ds)
#             Strongly Agree 23
#             Somewhat Agree 24
# Neither Agree nor Disagree 18
#          Somewhat Disagree 16
#          Strongly Disagree 19

# if you want to temporarily remove subtotals and headings, you can with `noTransforms`
noTransforms(crtabs(~opinion, ds))
#             Strongly Agree             Somewhat Agree Neither Agree nor Disagree
#                         23                         24                         18
#          Somewhat Disagree          Strongly Disagree
#                         16                         19
}

if (FALSE) {
# given a variable ds$opinion, with categories: Strongly Agree, Somewhat
# Agree, Neither Agree nor Disagree, Somewhat Disagree, and Strongly Disagree,
# to make two subtotals for Agree and Disagree:
subtotals(ds$opinion) <- list(
    Subtotal(
        name = "Agree",
        categories = c("Strongly Agree", "Somewhat Agree"),
        after = "Somewhat Agree"
    ),
    Subtotal(
        name = "Disagree",
        categories = c("Strongly Disagree", "Somewhat Disagree"),
        after = "Strongly Disagree"
    )
)

# headings can also be added:
subtotals(ds$opinion) <- Heading(name = "All opinions", position = "top")

# to see the subtotals and headings associated with a variable
subtotals(ds$opinion)
#        anchor         name     func    args
# 1      2        Agree subtotal 1 and 2
# 2      4     Disagree subtotal 4 and 5
# 3      0 All opinions     <NA>      NA

# when you use a variable with subtotals and headings in a cube, you see them
# by default
opinion_cube <- crtabs(~opinion, ds)
opinion_cube
#               All opinions
#             Strongly Agree 23
#             Somewhat Agree 24
#                      Agree 47
# Neither Agree nor Disagree 18
#          Somewhat Disagree 16
#          Strongly Disagree 19
#                   Disagree 35


# to get just the subtotals,
subtotalArray(opinion_cube)
#    Agree Disagree
#       47       35

# to remove all subtotals and headings
subtotals(ds$opinion) <- NULL
crtabs(~opinion, ds)
#             Strongly Agree 23
#             Somewhat Agree 24
# Neither Agree nor Disagree 18
#          Somewhat Disagree 16
#          Strongly Disagree 19

# if you want to temporarily remove subtotals and headings, you can with `noTransforms`
noTransforms(crtabs(~opinion, ds))
#             Strongly Agree             Somewhat Agree Neither Agree nor Disagree
#                         23                         24                         18
#          Somewhat Disagree          Strongly Disagree
#                         16                         19
}