View source: R/build_multiple_response_table.R
build_mtable | R Documentation |
This function can take one or two multiple response responses and generate a summary table with them. You can also cut these columns by other categorical columns by specify the cols parameter.
build_mtable(
x,
mcols,
cols = NULL,
table_title = "",
use_questions = FALSE,
use_NA = FALSE,
wt = NULL,
footnote = ""
)
x |
a data frame or tidy object. |
mcols |
the column(s) that are multiple response questions. See
the |
cols |
the column(s) that we want to calculate the sum/percentage of and the multiple response question. |
table_title |
the title of the table sheet |
use_questions |
if the data has column labels (was a imported .sav) file, convert the column label to a footnote with the question. |
use_NA |
logical. whether to include |
wt |
Specify a weighting variable, if |
footnote |
optional parameter to pass a custom footnote to the question,
this parameter overwrites |
A multiple response response is a series of columns with a single unique response that stores survey data where a respondent may have chosen multiple options. This function works if this data is stored in a wide format. To have a valid multiple response column all the columns should start with the same text, and each contain a unique value. That is it has the form:
data.frame(multi_col_1 = c(1,NA,1), multi_col_2 = c(1,1,1), multi_col_3 = c(NA,NA,1) ) #> multi_col_1 multi_col_2 multi_col_3 #> 1 1 1 NA #> 2 NA 1 NA #> 3 1 1 1
This is how popular survey platforms such as Qualtrics output this data type. If your data is long, you will need to pivot the data before hand, we recommend using pivot_wider.
By default this function converts labelled to a xlr_vector
by default (and underlying it is a character()
type).
This function and its family (build_table, build_qtable) is designed to
work with data with columns of type haven::labelled
,
which is the default format of data read with haven::read_sav
/has the format
of .sav
. .sav
is the default file function type of data from SPSS
and
can be exported from popular survey providers such as Qualtrics. When you
read in data with haven::read_sav
it imports data with the questions,
labels for the response options etc.
See labelled and read_sav if you would like more details on the importing type.
a xlr_table
object. Use write_xlsx to write to an Excel
file.
See xlr_table for more information.
library(xlr)
library(dplyr)
# You can use this function to calculate the number of people that have
# responded to the question `What is your favourite colour`
build_mtable(clothes_opinions,
"Q2",
table_title = "What is your favourite colour?")
# The function also lets you to see the number of NA questions (this is
# where someone doesn't answer any option)
build_mtable(clothes_opinions,
"Q2",
table_title = "What is your favourite colour?",
use_NA = TRUE)
# You can also cut all questions in the multiple response functions by another
# column
build_mtable(clothes_opinions,
"Q2",
gender2,
table_title = "Your favourite colour by gender")
# By setting `use_questions=TRUE` then the footnote will be the questions
# labels. This is useful to see what the question is.
# The function will try to pull out this based on the question label, and
# will manipulate try and get the correct label.
build_mtable(clothes_opinions,
"Q2",
gender2,
table_title = "Your favourite colour by gender",
use_questions = TRUE)
# It is common for your data to include 'other' responses in a multiple
# response column. You should remove the column before running build_mtable
clothes_opinions |>
select(-Q3_other) |>
build_mtable("Q3")
# You can also specify up to a maxium of two different multiple response
# columns.
clothes_opinions |>
select(-Q3_other) |>
build_mtable(c("Q2", "Q3"))
# These cam also be cut by other columns.
clothes_opinions |>
select(-Q3_other) |>
build_mtable(c("Q2", "Q3"),
gender2)
# This function also supports weights and manual footnotes
clothes_opinions |>
select(-Q3_other) |>
build_mtable(c("Q2", "Q3"),
gender2,
wt = weight,
footnote = "This is an example footnote.")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.