Recode Items, Factors and Numeric Vectors¶
Description¶
recode
substitutes old values of a factor or a numeric vector by new ones, just like the recoding facilities in some commercial statistical packages.
Usage¶
## S4 method for signature 'vector'
recode(x,...,otherwise="NA")
## S4 method for signature 'factor'
recode(x,...,otherwise="NA")
## S4 method for signature 'item'
recode(x,...,otherwise="NA")
Arguments¶
x

An object
...

One or more assignment expressions, each of the form
new.value < old.values
.new.value
should be a scalar numeric value or character string. If one of thenew.value``s is a character string, the return value of ``recode
will be a factor and eachnew.value
will be coerced to a character string that labels a level of the factor. Eachold.value
in an assignment expression may be a (numeric or character) vector. Ifx
is numeric such an assignment expression may have the formnew.value < range(lower,upper)
In that case, values betweenlower
andupper
are exchanged bynew.value
. If one of the arguments torange
ismin
, it is substituted by the minimum ofx
. If one of the arguments torange
ismax
, it is substituted by the maximum ofx
. In case of the method forlabelled
vectors, the tags of arguments of the formtag = new.value < old.values
will define the labels of the new codes. If theold.values
of different assignment expressions overlap, an error will be raised because the recoding is ambigous. otherwise

a character string or some other value that the result may obtain. If equal to
NA
or"NA"
, original codes not given an explicit new code are recoded intoNA
. If equal to"copy"
, original codes not given an explicit new code are copied.
Value¶
A numerical vector, factor or an item
object.
Details¶
recode
relies on the lazy evaluation mechanism of R: Arguments are not evaluated until required by the function they are given to. recode
does not cause arguments that appear in ...
to be evaluated. Instead, recode
parses the ...
arguments. Therefore, although expressions like 1 < 1:4
would cause an error action, if evaluated at any place elsewhere in R, they will not cause an error action, if given to recode
as an argument. However, a call of the form recode(x,1=1:4)
, would be a syntax error.
If John Fox’ package “car” is installed, recode
will also be callable with the syntax of the recode
function of that package.
See also¶
recode
of package “car”.
Examples¶
x < as.item(sample(1:6,20,replace=TRUE),
labels=c( a=1,
b=2,
c=3,
d=4,
e=5,
f=6))
print(x)
[1] f d e f c c d c a f a a b b a b e c c d
# A recoded version of x is returned
# containing the values 1, 2, 3, which are
# labelled as "A", "B", "C".
recode(x,
A = 1 < range(min,2),
B = 2 < 3:4,
C = 3 < range(5,max), # this last comma is ignored
)
Item (measurement: nominal, type: integer, length = 20)
[1:20] C B C C B B B B A C A A A A A A C B B B
# This causes an error action: the sets
# of original values overlap.
try(recode(x,
A = 1 < range(min,2),
B = 2 < 2:4,
C = 3 < range(5,max)
))
Error in recode(x, A = 1 < range(min, 2), B = 2 < 2:4, C = 3 < range(5, :
recoding request is ambiguous
recode(x,
A = 1 < range(min,2),
B = 2 < 3:4,
C = 3 < range(5,6),
D = 4 < 7
)
Warning in recode(x, A = 1 < range(min, 2), B = 2 < 3:4, C = 3 < range(5, :
recoding 4 < 7 has no consequences
Item (measurement: nominal, type: integer, length = 20)
[1:20] C B C C B B B B A C A A A A A A C B B B
# This results in an allmissing vector:
recode(x,
D = 4 < 7,
E = 5 < 8
)
Warning in recode(x, D = 4 < 7, E = 5 < 8) :
recodings 4 < 7, 5 < 8 have no consequences
Item (measurement: nominal, type: integer, length = 20)
[1:20] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
f < as.factor(x)
x < as.integer(x)
recode(x,
1 < range(min,2),
2 < 3:4,
3 < range(5,max)
)
[1] 3 2 3 3 2 2 2 2 1 3 1 1 1 1 1 1 3 2 2 2
# This causes another error action:
# the third argument is an invalid
# expression for a recoding.
try(recode(x,
1 < range(min,2),
3:4,
3 < range(5,max)
))
Error in recode(x, 1 < range(min, 2), 3:4, 3 < range(5, max)) :
invalid recoding request
# The new values are character strings,
# therefore a factor is returned.
recode(x,
"a" < range(min,2),
"b" < 3:4,
"c" < range(5,6)
)
[1] c b c c b b b b a c a a a a a a c b b b
Levels: a b c
recode(x,
1 < 1:3,
2 < 4:6
)
[1] 2 2 2 2 1 1 2 1 1 2 1 1 1 1 1 1 2 1 1 2
recode(x,
4 < 7,
5 < 8,
otherwise = "copy"
)
Warning in recode(x, 4 < 7, 5 < 8, otherwise = "copy") :
recodings 4 < 7, 5 < 8 have no consequences
[1] 6 4 5 6 3 3 4 3 1 6 1 1 2 2 1 2 5 3 3 4
recode(f,
"A" < c("a","b"),
"B" < c("c","d"),
otherwise="copy"
)
[1] f B e f B B B B A f A A A A A A e B B B
Levels: A B e f
recode(f,
"A" < c("a","b"),
"B" < c("c","d"),
otherwise="C"
)
[1] C B C C B B B B A C A A A A A A C B B B
Levels: A B C
recode(f,
"A" < c("a","b"),
"B" < c("c","d")
)
[1] <NA> B <NA> <NA> B B B B A <NA> A A A A A
[16] A <NA> B B B
Levels: A B