%%COMPUTE%% | R Documentation |
Easily compute multivariate sum, mean, and other scores.
Reverse scoring can also be easily implemented without saving extra variables.
Alpha
function uses a similar method to deal with reverse scoring.
Three options to specify variables:
var + items
: common and unique parts of variable names (suggested).
vars
: a character vector of variable names (suggested).
varrange
: starting and stopping positions of variables (NOT suggested).
COUNT(data, var = NULL, items = NULL, vars = NULL, varrange = NULL, value = NA) MODE(data, var = NULL, items = NULL, vars = NULL, varrange = NULL) SUM( data, var = NULL, items = NULL, vars = NULL, varrange = NULL, rev = NULL, range = likert, likert = NULL, na.rm = TRUE ) .sum( var = NULL, items = NULL, vars = NULL, varrange = NULL, rev = NULL, range = likert, likert = NULL, na.rm = TRUE ) MEAN( data, var = NULL, items = NULL, vars = NULL, varrange = NULL, rev = NULL, range = likert, likert = NULL, na.rm = TRUE ) .mean( var = NULL, items = NULL, vars = NULL, varrange = NULL, rev = NULL, range = likert, likert = NULL, na.rm = TRUE ) STD( data, var = NULL, items = NULL, vars = NULL, varrange = NULL, rev = NULL, range = likert, likert = NULL, na.rm = TRUE ) CONSEC( data, var = NULL, items = NULL, vars = NULL, varrange = NULL, values = 0:9 )
data |
Data frame. |
var |
[Option 1]
The common part across the variables. e.g., |
items |
[Option 1]
The unique part across the variables. e.g., |
vars |
[Option 2]
A character vector specifying the variables. e.g., |
varrange |
[Option 3]
A character string specifying the positions ("starting:stopping") of variables. e.g., |
value |
[Only for |
rev |
[Optional] Variables that need to be reversed. It can be (1) a character vector specifying the reverse-scoring variables (recommended), or (2) a numeric vector specifying the item number of reverse-scoring variables (not recommended). |
range, likert |
[Optional] Range of likert scale (e.g., |
na.rm |
Ignore missing values. Default is |
values |
[Only for |
A vector of computed values.
COUNT()
: Count a certain value across variables.
MODE()
: Compute mode across variables.
SUM()
: Compute sum across variables.
.sum()
: Tidy version of SUM
,
only can be used in add()/added()
MEAN()
: Compute mean across variables.
.mean()
: Tidy version of MEAN
,
only can be used in add()/added()
STD()
: Compute standard deviation across variables.
CONSEC()
: Compute consecutive identical digits across variables (especially useful in detecting careless responding).
d = data.table(x1=1:5, x4=c(2,2,5,4,5), x3=c(3,2,NA,NA,5), x2=c(4,4,NA,2,5), x5=c(5,4,1,4,5)) d ## I deliberately set this order to show you ## the difference between "vars" and "varrange". ## ====== Usage 1: data.table `:=` ====== ## d[, `:=`( na = COUNT(d, "x", 1:5, value=NA), n.2 = COUNT(d, "x", 1:5, value=2), sum = SUM(d, "x", 1:5), m1 = MEAN(d, "x", 1:5), m2 = MEAN(d, vars=c("x1", "x4")), m3 = MEAN(d, varrange="x1:x2", rev="x2", range=1:5), cons1 = CONSEC(d, "x", 1:5), cons2 = CONSEC(d, varrange="x1:x5") )] d ## ====== Usage 2: `add()` & `added()` ====== ## data = as.data.table(psych::bfi) added(data, { gender = as.factor(gender) education = as.factor(education) E = .mean("E", 1:5, rev=c(1,2), range=1:6) A = .mean("A", 1:5, rev=1, range=1:6) C = .mean("C", 1:5, rev=c(4,5), range=1:6) N = .mean("N", 1:5, range=1:6) O = .mean("O", 1:5, rev=c(2,5), range=1:6) }, drop=TRUE) data
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.