mcode: Multivariate recode

Description Usage Arguments Details Value See Also Examples

View source: R/mcode.R

Description

Recode one or more vectors to a single vector

Usage

1
2
mcode(..., recodes, .fill = NA, .result, .factors = c("character",
  "numeric"))

Arguments

...

One or more vectors of equal length.

recodes

A ‘car’-like set of recode commands.

.fill

A single value to use to fill in missing values in the resulting branched variables.

.result

A character vector specifying the class of the resulting vector.

.factors

A character string indicating whether to treatment factors in ... as character (the default) or numeric.

Details

Recoding is a basic step in any analysis. It is fairly easy to recode a single variable (e.g. by replacing values in the vector or using the recode function in car or mapvalues in plyr), but it can be cumbersome to recode multiple variables into a single vector .This is useful when, for example, a factorial experiment has the group for each factor stored as separate variables, but analysis will be performed across the entire design (rather than factor-by-factor), or when it is necessary to create a factor representing multivariate combinations of demographic groups (e.g., an age-sex-race stratification) from a set of separate input vectors representing each demographic variable. That would normally require a series of ifelse statements or complex use of boolean arguments. This function aims to make it simple to create a single vector from multiple input vectors in a manner more useful than interaction.

The syntax borrows from the recode function in the car package.

This really only works for categorical variables, but a continuous variable could be collapsed with a standard recode() command before being used with this.

Value

A vector of length equal to the input vector(s) in ....

See Also

mergeNA

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# RECODE A SINGLE VARIABLE BASED ON A `car::recode`-STYLE SCHEME
r <- mcode(c(1,3,5,4,2), recodes = "5=1;4=2;3=3;2=4;1=5")
stopifnot(identical(r, c(5,3,1,2,4)))

# WORK WITH MISSING VALUES:
mcode(c(1,1,1,1,1,NA), c(1,1,2,2,NA,1), recodes = "c(1,1)=1;c(1,2)=2;c(1,NA)=3")

# COMPARE `mcode` TO VARIOUS ALTERNATIVES
a <- c(1,2,1,2,1,NA,2,NA)
b <- c(1,1,2,2,NA,1,NA,2)

# recode using `mcode`
m1 <- mcode(a, b, recodes = "c(1,1)=1;c(1,2)=2;c(2,1)=3;c(2,2)=4")

# compare to `ifelse`:
m2 <- ifelse(a == 1 & b == 1, 1, 
             ifelse(a == 1 & b == 2, 2, 
                    ifelse(a == 2 & b == 1, 3, 
                           ifelse(a == 2 & b == 2, 4, NA))))
identical(m1, m2)

# compare to a sequence of extraction statements
m3 <- rep(NA, length(a))
m3[a == 1 & b == 1] <- 1
m3[a == 1 & b == 2] <- 2
m3[a == 2 & b == 1] <- 3
m3[a == 2 & b == 2] <- 4
identical(m1, m3)

# compare to interaction
m4 <- interaction(a, b)
levels(m4) <- c("1.1" = 1, "1.2" = 2, "2.1" = 3, "2.2" = 4)[levels(m4)]
m4 <- as.numeric(as.character(m4))
identical(m1, m4)

r <- "c(1,1,1,1)=1;c(1,1,1,0)=2;c(1,1,0,1)=3;c(1,0,1,1)=4;c(0,1,1,1)=5"
mcode(c(rep(1,9),0), 
      c(rep(0,5),rep(1,5)), 
      c(rep(1,8),0,1), 
      c(rep(1,5),rep(0,2),rep(1,3)), 
      recodes = r)

leeper/mcode documentation built on May 21, 2019, 12:37 a.m.