Description Usage Arguments Details Value Author(s) See Also Examples
A flexible function for recoding a variable. It can be used for anything from simple explicit recoding to piecewise-defined transformations.
1 |
var |
|
recode |
|
other |
When |
na |
When |
as.what |
if |
recode
contains the instructions for how the recoding is done. Each instruction is in the form of condition: newvalue
and instrunctions must be separated by semicolons (;
).
E.g., 1:2; 2:-1; 3:0
will change all ones to two, twos to negative one, and threes to zero. Instruction can include compound conditions and functions for newvalue expressed in terms of $
. For example, "<0:0;
>2:4;
>=0 & <=2 : $^2
" will give zero for all the values below 0, 4 for all the values above 2, and squared values for anything between 0 and 2.
The input can be other types beside numeric. For characters and factors, it is important that the form "old":"new"
includes quotations around both the left-hand side and the right-hand side.
When the input is a factor, recoding works as expected, but new levels cannot be assigned. In order to assign new levels, the input vector should be made into something else (e.g., a character) and then the argument as.what
could be set to "factor"
to get a factor as the end result.
The recoded vector is returned.
Ali Sanaei
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 42 | u = rnorm(1000)
u.2 = recoder(u,'<-2: -2; >2 : 2' ,other = 0)
plot(u,u.2)
# use a function of 'x' for assigning new values based on old values
u.3 = recoder(u,'<-2: -2; >2 : 2; >=-2 & <=2 : $^3/4')
# NOTE: instructions in recode are executed from left to right
# So u.3 and u.4 are the same
u.4 = recoder(u,'<-2: -2; >=-2 : $^3/4; >2 : 2')
plot(u, u.3)
# another example of a piecewise defined function
x = runif(1000,-1,10)
y = recoder(x,'>0 & <1: sqrt($); >=1 : 1+log($)' ,other=NA)
plot(y~x)
table(is.na(y))
# we can also create other types of variables
k = rpois(n = 10000, lambda = 3)
evodd = recoder(k %% 2, '0: "even"; 1: "odd" ')
table(evodd)
# the output could also be made a factor, etc., using "as.what" argument
evodd.factor = recoder(k %% 2, '0: "even"; 1: "odd" ', as.what='factor')
# characters are recoded similarly
str = c('a','b','x','z',NA)
str2 = recoder(str, '"x":"c"; "z":"d" ')
str3 = recoder(str, '"a":1; "b":2; "x":3; "z":4 ')
print(str3) #note that this is still not numeric
num3 = recoder(str, '"a":1; "b":2; "x":3; "z":4 ', as.what='numeric')
print(num3)
# when input is a factor, the levels are fixed
f1 = factor( c('a','b','x','z',NA) )
f2 = recoder( f1, '"x":"a"; "z":"b" ')
## Not run:
f3 = recoder(f1, '"x":"c"; "z":"d" ') # "c" and "d" are not recognized
## End(Not run)
f3 = recoder( as.character(f1), '"x":"c"; "z":"d" ' , as.what= "factor")
|
Loading required package: stringr
FALSE TRUE
912 88
evodd
even odd
5087 4913
[1] "1" "2" "3" "4" NA
[1] 1 2 3 4 NA
Warning messages:
1: In `[<-.factor`(`*tmp*`, index, value = "c") :
invalid factor level, NA generated
2: In `[<-.factor`(`*tmp*`, index, value = "d") :
invalid factor level, NA generated
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.