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
# 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
# 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
}
Run the code above in your browser using DataLab