Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

grapes-grapes-COMPUTE-grapes-grapes

Multivariate computation.


Description

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 the variable list:

  1. var + items: use the common and unique parts of variable names.

  2. vars: directly define a variable list.

  3. varrange: use the start and end positions of a variable list.

Usage

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,
  likert = NULL,
  na.rm = TRUE
)

MEAN(
  data,
  var = NULL,
  items = NULL,
  vars = NULL,
  varrange = NULL,
  rev = NULL,
  likert = NULL,
  na.rm = TRUE
)

STD(
  data,
  var = NULL,
  items = NULL,
  vars = NULL,
  varrange = NULL,
  rev = NULL,
  likert = NULL,
  na.rm = TRUE
)

CONSEC(
  data,
  var = NULL,
  items = NULL,
  vars = NULL,
  varrange = NULL,
  values = 0:9
)

Arguments

data

Data frame.

var

[option 1] Common part across multiple variables (e.g., "RSES", "SWLS").

items

[option 1] Unique part across multiple variables (e.g., 1:10).

vars

[option 2] Character vector specifying a variable list (e.g., c("E1", "E2", "E3", "E4", "E5")).

varrange

[option 3] Character with ":" specifying the start and end positions of a variable list (e.g., "A1:E5").

value

[only for COUNT] The value to be counted.

rev

[optional] Reverse-scoring variables. It can be (1) a numeric vector specifying the positions of reverse-scoring variables (not recommended) or (2) a character vector directly specifying the variable list (recommended).

likert

[optional] Range of likert scale (e.g., 1:5, c(1, 5)). If not provided, it will be automatically estimated from the given data (BUT you should use this carefully).

na.rm

Ignore missing values. Default is TRUE.

values

[only for CONSEC] Values to be counted as consecutive identical values. Default is all numbers (0:9).

Value

A vector of computed values.

Functions

  • COUNT: Count a certain value across multiple variables.

  • MODE: Compute mode across multiple variables.

  • SUM: Compute sum across multiple variables.

  • MEAN: Compute mean across multiple variables.

  • STD: Compute standard deviation across multiple variables.

  • CONSEC: Compute consecutive identical digits across multiple variables (especially useful in detecting careless responding).

Examples

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".

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", likert=1:5),
  cons1=CONSEC(d, "x", 1:5),
  cons2=CONSEC(d, varrange="x1:x5")
)]
d

data=as.data.table(bfi)
data[,`:=`(
  E=MEAN(d, "E", 1:5, rev=c(1,2), likert=1:6),
  O=MEAN(d, "O", 1:5, rev=c(2,5), likert=1:6)
)]
data

bruceR

Broadly Useful Convenient and Efficient R Functions

v0.6.2
GPL-3
Authors
Han-Wu-Shuang Bao [aut, cre]
Initial release
2021-04-08

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.