Description Usage Arguments Details Author(s) See Also Examples
recode
easily manipulates contents of a new variable within dataframe
1 |
data |
data frame |
var |
name of a new variable |
old.value |
vector |
new.value |
vector (length of 1 or same with old.value) |
recode
changes the values of variables including categorical variables
according to the rules specified below.
In case of factor, recode
first converts the vector into character,
recodes and then revert back to factor.
If data is specified, it returns the whole dataframe with recoded variables.
Common rules:
Old.value New.value Example Meaning
# # 0 >>> 1 0 recoded to 1
c(#, #) c(#, #) c(1,2) >>> c(3,4) 1 to 3; 2 to 4
c(#, #) # c(1,2) >>> 1 1 to 1; 2 to 1
#:# # 1:5 >>> 1 1 to 1; 2 to 1; ... ; 5 to 1
Myo Minn Oo (Email: dr.myominnoo@gmail.com | Website: https://myominnoo.github.io/)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | ## Not run:
table(infert$case)
infert.new <- recode(infert, case, 0, 2)
table(infert.new$case)
infert.new <- recode(infert.new, case, c(1, 2), c(3, 4))
table(infert.new$case)
table(infert$parity)
infert.new <- recode(infert, parity, c(1,2,3,4), 0)
table(infert.new$parity)
table(infert$age)
infert.new <- recode(infert, age, 21:30, 1)
infert.new <- recode(infert.new, age, 31:40, 2)
infert.new <- recode(infert.new, age, 41:44, 3)
table(infert.new$age)
# errors
recode(infert, age, 21:30, c(1, 2))
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.