| 1 |
\name{recode} |
| 2 |
\alias{recode} |
| 3 |
\alias{recode,vector-method} |
| 4 |
\alias{recode,item-method} |
| 5 |
\alias{recode,factor-method} |
| 6 |
\title{Recode Items, Factors and Numeric Vectors} |
| 7 |
\description{ |
| 8 |
\code{recode} substitutes old values of a factor or a numeric |
| 9 |
vector by new ones, just like the recoding facilities in some |
| 10 |
commercial statistical packages. |
| 11 |
} |
| 12 |
\usage{ |
| 13 |
recode(x,\dots, |
| 14 |
copy=getOption("recode_copy",identical(otherwise,"copy")), |
| 15 |
otherwise=NA) |
| 16 |
\S4method{recode}{vector}(x,\dots, |
| 17 |
copy=getOption("recode_copy",identical(otherwise,"copy")), |
| 18 |
otherwise=NA) |
| 19 |
\S4method{recode}{factor}(x,\dots, |
| 20 |
copy=getOption("recode_copy",identical(otherwise,"copy")), |
| 21 |
otherwise=NA) |
| 22 |
\S4method{recode}{item}(x,\dots, |
| 23 |
copy=getOption("recode_copy",identical(otherwise,"copy")), |
| 24 |
otherwise=NA) |
| 25 |
} |
| 26 |
\arguments{ |
| 27 |
\item{x}{An object} |
| 28 |
\item{\dots}{One or more assignment expressions, each |
| 29 |
of the form \code{new.value <- old.values}. |
| 30 |
\code{new.value} should be a scalar numeric value |
| 31 |
or character string. If one of the \code{new.value}s |
| 32 |
is a character string, the return value |
| 33 |
of \code{recode} will be a factor and each \code{new.value} |
| 34 |
will be coerced to a character string that labels a level of the factor. |
| 35 |
|
| 36 |
Each \code{old.value} in an assignment expression may be a |
| 37 |
(numeric or character) vector. If \code{x} is numeric such an |
| 38 |
assignment expression may have the form \code{new.value <- range(lower,upper)} |
| 39 |
In that case, values between \code{lower} and \code{upper} are exchanged by |
| 40 |
\code{new.value}. If one of the arguments to \code{range} is \code{min}, |
| 41 |
it is substituted by the minimum of \code{x}. |
| 42 |
If one of the arguments to \code{range} is \code{max}, |
| 43 |
it is substituted by the maximum of \code{x}. |
| 44 |
|
| 45 |
In case of the method for \code{labelled} vectors, the \emph{tags} of |
| 46 |
arguments of the form \code{tag = new.value <- old.values} |
| 47 |
will define the labels of the new codes. |
| 48 |
|
| 49 |
If the \code{old.values} of different assignment expressions overlap, |
| 50 |
an error will be raised because the recoding is ambigous. |
| 51 |
} |
| 52 |
\item{copy}{logical; should those values of \code{x} not given an |
| 53 |
explicit new code copied into the resulting vector?} |
| 54 |
\item{otherwise}{a character string or some other value |
| 55 |
that the result may obtain. If equal to \code{NA} or \code{"NA"}, |
| 56 |
original codes not given an explicit new code are recoded into |
| 57 |
\code{NA}. If equal to \code{"copy"}, |
| 58 |
original codes not given an explicit new code are copied. |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
\value{ |
| 63 |
A numerical vector, factor or an \code{item} object. |
| 64 |
} |
| 65 |
|
| 66 |
\details{ |
| 67 |
\code{recode} relies on the lazy evaluation mechanism of \emph{R}: |
| 68 |
Arguments are not evaluated until required by the function they are given to. |
| 69 |
\code{recode} does not cause arguments that appear in \code{\dots} to be evaluated. |
| 70 |
Instead, \code{recode} parses the \code{\dots} arguments. Therefore, although |
| 71 |
expressions like \code{1 <- 1:4} would cause an error action, if evaluated |
| 72 |
at any place elsewhere in \emph{R}, they will not cause an error action, |
| 73 |
if given to \code{recode} as an argument. However, a call of the |
| 74 |
form \code{recode(x,1=1:4)}, would be a syntax error. |
| 75 |
|
| 76 |
If John Fox' package "car" is installed, \code{recode} will also be callable |
| 77 |
with the syntax of the \code{recode} function of that package. |
| 78 |
} |
| 79 |
|
| 80 |
\seealso{ |
| 81 |
\code{recode} of package "car". |
| 82 |
} |
| 83 |
|
| 84 |
\examples{ |
| 85 |
x <- as.item(sample(1:6,20,replace=TRUE), |
| 86 |
labels=c( a=1, |
| 87 |
b=2, |
| 88 |
c=3, |
| 89 |
d=4, |
| 90 |
e=5, |
| 91 |
f=6)) |
| 92 |
print(x) |
| 93 |
|
| 94 |
|
| 95 |
codebook( |
| 96 |
recode(x, |
| 97 |
a = 1 <- 1:2, |
| 98 |
b = 2 <- 4:6)) |
| 99 |
|
| 100 |
|
| 101 |
codebook( |
| 102 |
recode(x, |
| 103 |
a = 1 <- 1:2, |
| 104 |
b = 2 <- 4:6, |
| 105 |
copy = TRUE)) |
| 106 |
|
| 107 |
# Note the handling of labels if the recoding rules are bijective |
| 108 |
codebook( |
| 109 |
recode(x, |
| 110 |
1 <- 2, |
| 111 |
2 <- 1, |
| 112 |
copy=TRUE)) |
| 113 |
|
| 114 |
codebook( |
| 115 |
recode(x, |
| 116 |
a = 1 <- 2, |
| 117 |
b = 2 <- 1, |
| 118 |
copy=TRUE)) |
| 119 |
|
| 120 |
|
| 121 |
# A recoded version of x is returned |
| 122 |
# containing the values 1, 2, 3, which are |
| 123 |
# labelled as "A", "B", "C". |
| 124 |
recode(x, |
| 125 |
A = 1 <- range(min,2), |
| 126 |
B = 2 <- 3:4, |
| 127 |
C = 3 <- range(5,max), # this last comma is ignored |
| 128 |
) |
| 129 |
|
| 130 |
# This causes an error action: the sets |
| 131 |
# of original values overlap. |
| 132 |
try(recode(x, |
| 133 |
A = 1 <- range(min,2), |
| 134 |
B = 2 <- 2:4, |
| 135 |
C = 3 <- range(5,max) |
| 136 |
)) |
| 137 |
|
| 138 |
recode(x, |
| 139 |
A = 1 <- range(min,2), |
| 140 |
B = 2 <- 3:4, |
| 141 |
C = 3 <- range(5,6), |
| 142 |
D = 4 <- 7 |
| 143 |
) |
| 144 |
|
| 145 |
# This results in an all-missing vector: |
| 146 |
recode(x, |
| 147 |
D = 4 <- 7, |
| 148 |
E = 5 <- 8 |
| 149 |
) |
| 150 |
|
| 151 |
f <- as.factor(x) |
| 152 |
x <- as.integer(x) |
| 153 |
|
| 154 |
recode(x, |
| 155 |
1 <- range(min,2), |
| 156 |
2 <- 3:4, |
| 157 |
3 <- range(5,max) |
| 158 |
) |
| 159 |
|
| 160 |
# This causes another error action: |
| 161 |
# the third argument is an invalid |
| 162 |
# expression for a recoding. |
| 163 |
try(recode(x, |
| 164 |
1 <- range(min,2), |
| 165 |
3:4, |
| 166 |
3 <- range(5,max) |
| 167 |
)) |
| 168 |
|
| 169 |
# The new values are character strings, |
| 170 |
# therefore a factor is returned. |
| 171 |
recode(x, |
| 172 |
"a" <- range(min,2), |
| 173 |
"b" <- 3:4, |
| 174 |
"c" <- range(5,6) |
| 175 |
) |
| 176 |
|
| 177 |
recode(x, |
| 178 |
1 <- 1:3, |
| 179 |
2 <- 4:6 |
| 180 |
) |
| 181 |
|
| 182 |
recode(x, |
| 183 |
4 <- 7, |
| 184 |
5 <- 8, |
| 185 |
otherwise = "copy" |
| 186 |
) |
| 187 |
|
| 188 |
recode(f, |
| 189 |
"A" <- c("a","b"), |
| 190 |
"B" <- c("c","d"), |
| 191 |
otherwise="copy" |
| 192 |
) |
| 193 |
|
| 194 |
recode(f, |
| 195 |
"A" <- c("a","b"), |
| 196 |
"B" <- c("c","d"), |
| 197 |
otherwise="C" |
| 198 |
) |
| 199 |
|
| 200 |
recode(f, |
| 201 |
"A" <- c("a","b"), |
| 202 |
"B" <- c("c","d") |
| 203 |
) |
| 204 |
|
| 205 |
DS <- data.set(x=as.item(sample(1:6,20,replace=TRUE), |
| 206 |
labels=c( a=1, |
| 207 |
b=2, |
| 208 |
c=3, |
| 209 |
d=4, |
| 210 |
e=5, |
| 211 |
f=6))) |
| 212 |
print(DS) |
| 213 |
|
| 214 |
DS <- within(DS,{ |
| 215 |
xf <- recode(x, |
| 216 |
"a" <- range(min,2), |
| 217 |
"b" <- 3:4, |
| 218 |
"c" <- range(5,6) |
| 219 |
) |
| 220 |
xn <- [email protected] |
| 221 |
xc <- recode(xn, |
| 222 |
"a" <- range(min,2), |
| 223 |
"b" <- 3:4, |
| 224 |
"c" <- range(5,6) |
| 225 |
) |
| 226 |
xc <- as.character(x) |
| 227 |
xcc <- recode(xc, |
| 228 |
1 <- letters[1:2], |
| 229 |
2 <- letters[3:4], |
| 230 |
3 <- letters[5:6] |
| 231 |
) |
| 232 |
}) |
| 233 |
|
| 234 |
DS |
| 235 |
|
| 236 |
DS <- within(DS,{ |
| 237 |
xf <- recode(x, |
| 238 |
"a" <- range(min,2), |
| 239 |
"b" <- 3:4, |
| 240 |
"c" <- range(5,6) |
| 241 |
) |
| 242 |
x1 <- recode(x, |
| 243 |
1 <- range(1,2), |
| 244 |
2 <- range(3,4), |
| 245 |
copy=TRUE |
| 246 |
) |
| 247 |
xf1 <- recode(x, |
| 248 |
"A" <- range(1,2), |
| 249 |
"B" <- range(3,4), |
| 250 |
copy=TRUE |
| 251 |
) |
| 252 |
}) |
| 253 |
DS |
| 254 |
codebook(DS) |
| 255 |
|
| 256 |
DF <- data.frame(x=rep(1:6,4,replace=TRUE)) |
| 257 |
DF <- within(DF,{ |
| 258 |
xf <- recode(x, |
| 259 |
"a" <- range(min,2), |
| 260 |
"b" <- 3:4, |
| 261 |
"c" <- range(5,6) |
| 262 |
) |
| 263 |
x1 <- recode(x, |
| 264 |
1 <- range(1,2), |
| 265 |
2 <- range(3,4), |
| 266 |
copy=TRUE |
| 267 |
) |
| 268 |
xf1 <- recode(x, |
| 269 |
"A" <- range(1,2), |
| 270 |
"B" <- range(3,4), |
| 271 |
copy=TRUE |
| 272 |
) |
| 273 |
xf2 <- recode(x, |
| 274 |
"B" <- range(3,4), |
| 275 |
"A" <- range(1,2), |
| 276 |
copy=TRUE |
| 277 |
) |
| 278 |
}) |
| 279 |
DF |
| 280 |
codebook(DF) |
| 281 |
|
| 282 |
} |
| 283 |
\keyword{manip} |