knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
A panoply of miscellaneous functions: column_find
, column_alpha
, column_combine
, capply
, scuttle
, spround
, perble
, lenique
, pasterisk
, bolder
, collapse
, paste_paren
, paste_ci
, centre
, reverse
, mat_merge
, delta_rsq
, delta_aic
, delta_bic
, group_compare
, zo
, reorder
, build_models
, text_format
(including bold
, bold_tex
, italic
, and italic_tex
), and dark
(including dark_triad
and dark_tetrad
). scuttle
was created in collaboration with AshLynnMiller. A large debt of gratitude is also owed to datalorax and his functional programming course. His instruction, course materials, and feedback were instrumental in creating this package.
The development version of panoply
can be installed from GitHub with:
# install.packages("devtools") devtools::install_github("camkay/panoply")
# load panoply library(panoply) # suppress scientific notation options(scipen = 999) # create example data data_example <- data.frame(scale1_item1 = c(6, 1, 3, 4, 5, 9, 9), scale1_item2 = c(7, 2, 4, 5, 4, 8, 9), scale1_item3 = c(8, 1, 5, 4, 4, 9, 8), scale2_item1 = c(9, 9, 9, 8, 4, 2, 2), scale2_item2 = c(7, 8, 7, 9, 5, 1, 2)) data_example_2 <- data.frame(group = rep(c("A", "A", "B", "B", "A"), 2), mach = rep(c(NA, 2, 300, 200, 3), 2), narc = rep(c(2, 4, 500, 700, 10), 2), psyc = rep(c(3, 4, 1800, 2000, 5), 2)) data_example_3 <- data.frame(group = c("A", "B", "A", "A", "B", "B", "A", "B", "B", "A"), mach = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), narc = c(10, 20, 35, 50, 65, 71, 80, 96, 99, 90), psyc = c(-1, -7, -1, -1, -3, -4, 1, -9, -9, -1)) data_example_5 <- data.frame(item1 = c(6, 1, 3, 4), item2 = c(7, 2, 4, 5), item3 = c(8, 3, 5, 6), order_1 = c("item1|item2|item3", "item3|item2|item1", "item1|item3|item2", "item3|item1|item2"), order_2 = c("item3+item2+item1", "item1+item2+item3", "item3+item1+item2", "item1+item3+item2"), order_3 = c("i3+i2+i1", "i1+i2+i3", "i3+i1+i2", "i1+i3+i2")) # create example numeric vector num_example <- c(5, 1, 0, .5, .1, .05, .01, .005, .001, .0005) # create example character vector char_example <- c("cat", "cat", "dog", "cat", "dog", "giraffe") # create example mats mat_a <- matrix(rep( "apple", 25), nrow = 5) mat_a[lower.tri(mat_a)] <- matrix(rep("orange", 25), nrow = 5)[lower.tri(mat_a)] mat_b <- matrix(rep( "banana", 25), nrow = 5) mat_b[lower.tri(mat_b)] <- matrix(rep("kiwi", 25), nrow = 5)[lower.tri(mat_b)] # create example models mod_a_example <- lm(scale1_item1 ~ scale2_item1, data = data_example) mod_b_example <- lm(scale1_item1 ~ scale2_item1 + scale2_item2, data = data_example) mod_c_example <- lm(scale1_item1 ~ scale2_item1 + scale2_item2 + scale1_item3, data = data_example)
column_find
is a function for quickly identifying columns of a data frame that match a pattern
specified through a string. The default behaviour is to return a logical vector, indicating the columns that match the pattern
. Using the return
argument, users can request that a vector of column numbers ("numeric") or column names ("character") be returned. Users can also request that a data frame with only those columns be returned ("data.frame"). invert
works for all return types, identifying or extracting the columns that DO NOT match the pattern. antipattern
allows you to exclude columns from the matched output. column_find
can interpret regular expressions.
# look at example data data_example # return a logical vector of the columns that match the pattern column_find(pattern = "scale1", return = "logical", data = data_example) # return a numeric vector of the columns that match the pattern column_find(pattern = "scale1", return = "numeric", data = data_example) # return a character vector of the columns that match the pattern column_find(pattern = "scale1", return = "character", data = data_example) # return a data frame of the columns that match the pattern column_find(pattern = "scale1", return = "data.frame", data = data_example) # return a data frame of the columns that DO NOT match the pattern column_find(pattern = "scale1", return = "data.frame", data = data_example, invert = FALSE) # return a logical vector using a regular expression column_find(pattern = "2$", return = "logical", data = data_example) # return a character vector that matches `scale1` but not `item2` column_find(pattern = "scale1", return = "character", data = data_example, antipattern = "item2")
column_alpha
estimates Cronbach's Alpha–an indicator of internal consistency–using only columns that have names that match a pattern
. The analysis relies on psych::alpha
. If the full
argument is TRUE
, the full results of the reliability analysis produced by the psych
package is returned. If FALSE
, only the raw alpha value is returned. In both cases, a message is generated informing the users what columns were used to calculate the alpha value.
# look at example data data_example # return the full reliability analysis for scale 1 column_alpha(pattern = "scale1", full = TRUE, data = data_example) # return only the raw Cronbach's Alpha for scale 1 column_alpha(pattern = "scale1", full = FALSE, data = data_example)
column_combine
creates a composite column using only columns in a data frame that have names that match a pattern
. The argument fun
specifies specifies what function should be used to create the composite column. Averaging is the default.
# look at example data data_example # return a vector of rowwise means for scale 1 column_combine(pattern = "scale1", fun = mean, data = data_example) # return a vector of rowwise sums for scale 1 column_combine(pattern = "scale1", fun = sum, data = data_example)
capply
is a wrapper of apply
that allows the user to quickly apply a function to every cell of a data frame.
# look at example data data_example # add 100 to every cell of data_example capply(data_example, function(x) x + 100)
scuttle
turns a continuous (i.e., numeric) variable into a categorical (i.e., character or factor) variable. Using the split
argument, users can specify whether they want a (1) quantile-split, (2) split at 1, 2, or 3 standard deviations above or below the mean, (3) split at 1, 2, or 3 standard errors above or below the mean. Users can specify whether the output should be a factor or a character using the as.factor
argument.
# look at example data data_example # split scale1_item1 using a quantile-split method and return a factor scuttle(column = data_example$scale1_item1, split = "quantile", as.factor = TRUE) # split scale1_item1 using a 1sd-split method and return a character vector scuttle(column = data_example$scale1_item1, split = "sd1", as.factor = FALSE)
spround
rounds a number or a vector of numbers (using round
) and specifies knitted decimal places (using sprintf
) all in one step. Users can specify whether leading zeroes should be retained or not using leading0
.
# look at example numeric vector num_example # round num_example to three decimal places and retain leading zeroes spround(x = num_example, digits = 3, leading0 = TRUE) # round num_example to three decimal places and drop leading zeroes spround(x = num_example, digits = 3, leading0 = FALSE)
perble
extends table
by including proportions and percentages. By default the results are put into tidy format. Results will be returned as a numeric matrix if the tidy
argument is set to FALSE
.
# look at example character vector char_example # produce the counts, proportions, and percentages and present the results in tidy format perble(x = char_example, tidy = TRUE) # produce the counts, proportions, and percentages and present the results as a numeric matrix perble(x = char_example, tidy = FALSE)
lenique
is a very simple wrapper that calculates the length of unique values in a vector in one step. It is identical to running length(unique(x))
.
# look at the example character vector char_example # calculate the length of unique values in char_example lenique(x = char_example)
pasterisk
takes a scalar or atomic vector of, for example, p-values and returns a scalar or atomic vector of asterisks corresponding to different significance levels. The argument thresholds
can be used to set the cut-off valuess for the different values. Any number of thresholds can be set. By default, an asterisk (i.e., "*") is used as the sig_symbol
, but any single character vector can be used.
# look at the example numeric vector num_example # create a vector of asterisks using the default thresholds pasterisk(p_vals = num_example, thresholds = c(0.05, 0.01, 0.001), sig_symbol = "*") # create a vector of octothorps/hashtags/pound using the default thresholds pasterisk(p_vals = num_example, thresholds = c(0.05, 0.01, 0.001), sig_symbol = "#") # create a vector of asterisks using custom thresholds pasterisk(p_vals = num_example, thresholds = c(0.10, 0.5), sig_symbol = "*")
bolder
takes a scalar or atomic vector of effect sizes and bolds values that are equal to or larger than a specified size.
# look at the example numeric vector num_example # bold values that are larger than .30 bolder(ef = num_example, threshold = .30) # bold values that are larger than 1.00 bolder(ef = num_example, threshold = 1.00)
A shortcut of paste(x, collapse = y)
for collapsing character vectors into a single string separated by some character. For example, running collapse(c("item 1", "item 2"), sep = " + ")
is identical to running paste(c("item 1", "item 2"), collapse = " + ")
.
# collapse a vector of item labels with the default separator collapse(c("item1", "item2", "item3")) # collapse a vector of item labels with spaces collapse(c("item1", "item2", "item3"), sep = " ")
If only x
is provided, paste_paren
wraps the value in parentheses (e.g., "10.12" becomes "(10.12)"). If x
and y
are both provided, it combines the two values (e.g., "10.12" and "2.22") by wrapping the latter in parentheses (e.g., "10.12 (2.22)"). This part of the function was made to streamline the creation of tables that include cells formatted mean(sd).
# wrap a single number in parentheses paste_paren(10.12) # combine two numbers paste_paren(10.12, 2.22)
paste_ci
combines two numbers (e.g., .20 and .33) by wrapping them in square brackets (e.g., [.20, .33]). This function was made to streamline the creation of confidence interval values for outputting.
# combine two numbers paste_ci(.20, .33)
mat_merge
combines two matrices by drawing values from either below or above the diagonal and placing them either below and above the diagonal.
# look at matrix a mat_a # look at matrix b mat_b # merge by drawing values from below the diagonal of both matrices mat_merge(mat_a, mat_b, x_from = "lower", y_from = "lower", x_to = "lower", y_to = "upper") # merge by drawing values from below the diagonal of mat_a and above the # diagonal of mat_b mat_merge(mat_a, mat_b, x_from = "lower", y_from = "upper", x_to = "lower", y_to = "upper") # identical to previous mat_merge but put the values into the opposite # quadrant of the matrix mat_merge(mat_a, mat_b, x_from = "lower", y_from = "upper", x_to = "upper", y_to = "lower")
centre
(or center
) centres (or centers) a numeric vector. It is identical to running 'scale(x, center = TRUE, scale = FALSE)'.
# look at num_example num_example # centre num_example centre(num_example)
reverse
reverse scores a value based on (1) the value (i.e., x
), (2) the lowest possible value (i.e., low
), and (3) the highest possible value (i.e., high
).
# reverse score 2 on a scale from 1 to 5 reverse(2, 1, 5) # reverse score -1 on a scale from -2 to 2 reverse(-1, -2, 2)
delta_rsq
, delta_aic
, and delta_bic
calculate the change to R-Squared, AIC, and BIC across two or more models.
# calculate delta r-squared delta_rsq(models = list(mod_a_example, mod_b_example, mod_c_example)) # calculate delta adjusted r-squared delta_rsq(models = list(mod_a_example, mod_b_example, mod_c_example), adjusted = TRUE) # calculate delta AIC delta_aic(models = list(mod_a_example, mod_b_example, mod_c_example)) # calculate delta BIC delta_bic(models = list(mod_a_example, mod_b_example, mod_c_example))
group_compare
creates a group comparison table. It (1) calculates an overall mean and sd, as well as a mean and sd for each group, (2) runs a two-samples t-test comparing both groups, and (3) calculates Cohen's d.
# look at example data data_example_2 # create group comparison table from example data group_compare(data_example_2, cols = c("mach", "narc"), split = "group") # create group comparison table, rounding and collapsing group_compare(data_example_2, cols = c("mach", "narc"), split = "group", spround = TRUE, collapse = TRUE)
zo
creates a zero-order correlation table. It allows you to specify a grouping variable (split
) and calculates a different correlation matrix above and below the diagonal.
# look at example data data_example_3 # create a zero-order correlation table zo(data_example_3, cols = c("mach", "narc", "psyc"), split = "group") # create a zero-order correlation table for manipulation zo(data_example_3, cols = c("mach", "narc", "psyc"), split = "group", pasterisk = FALSE)
reorder
reorders columns based on a string with separators. reorder
defaults to splitting using "\\|"
, because that is what is used by qualtrics.
# look at example data data_example_5 # reorder that data using a string with `|` separators reorder(data_example_5, "order_1") # reorder that data using a string with `+` separators reorder(data_example_5, "order_2", sep = "\\+")
build_models
takes an outcome string (i.e., outcome
) and a list of predictor strings (i.e., predictors
) and builds a set of models.
# create a set of linear models build_models(outcome = "y", predictors = list("1", "x1", "x2")) # create a set of linear models with predictors added simultaneously build_models(outcome = "y", predictors = list("1", "x1", c("z1", "z2"))) # create a set of linear models with interactions build_models(outcome = "y", predictors = list("1", "x1", c("z1", "z2"), c("x1 * z1", "x1 * z2"))) # create a set of linear mixed-effects models build_models(outcome = "y", predictors = list("1 + (1 |id)", "x1", "x2"))
text_format
formats strings as bold or italicized using markdown or LaTeX syntax. bold(x)
is a shortcut for text_format(x, format = "bold", latex = FALSE)
and italic(x)
is a shortcut for text_format(x, format = "italic", latex = FALSE)
. bold_tex(x)
is a shortcut for text_format(x, format = "bold", latex = TRUE)
and italic_tex(x)
is a shortcut for text_format(x, format = "italic", latex = TRUE)
.
# look at example strings char_example # italicize strings using markdown formatting text_format(char_example, format = "italic", latex = FALSE) #or italic(char_example) # italicize strings using latex formatting text_format(char_example, format = "italic", latex = TRUE) #or italic_tex(char_example) # bold strings using markdown formatting text_format(char_example, format = "bold", latex = FALSE) #or bold(char_example) # bold strings using latex formatting text_format(char_example, format = "bold", latex = TRUE) #or bold_tex(char_example) # super- and subscript strings using markdown formatting text_format(char_example, format = "super", latex = FALSE) text_format(char_example, format = "sub", latex = FALSE) # super- and subscript strings using latex formatting text_format(char_example, format = "super", latex = TRUE) text_format(char_example, format = "sub", latex = TRUE)
dark
returns a vector of aversive personality trait names (e.g., the Dark Triad or Dark Tetrad traits).
# return the dark triad traits dark("triad") # you can also use the dark_triad shortcut dark_triad() # return the dark tetrad traits dark("tetrad") # you can also use the dark_tetrad shortcut dark_tetrad() # if you want shortened names, you can set shorten to `TRUE` dark("triad", shorten = TRUE) # and if you want it to be shortened to some other length, `shorten_length` dark("triad", shorten = TRUE, shorten_length = 3) # maybe you want it in italics... dark("triad", format = "italic") # ...or bold dark("triad", format = "bold") # ...or in latex italics dark("triad", format = "italic", latex = TRUE)
first_diff
takes a numeric vector and returns a scalar representing the difference between the first element and the sum of all of the other elements.
# look at the example numeric vector num_example # manually calculate the difference between the first element and the sum of all of the other elements num_example[1] - sum(num_example[-1], na.rm = TRUE) # automatically calculate the difference between the first element and the sum of all of the other elements first_diff(num_example)
states_qualtrics
is a dataframe containing the state names (state_label
) that correspond to the numeric state values (state_value
) used by Qualtrics.
# look at the dataframe
states_qualtrics
d
is a very simple wrapper for the describe()
function from the {psych}
package. In addition to having a shorter function name, it also returns fewer statistics by default.
# look at the example numeric vector num_example # calculate statistics d(num_example)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.