recode: Recode contents of a variable

Description Usage Arguments Details Author(s) See Also Examples

View source: R/recode.R

Description

recode easily manipulates contents of a new variable within dataframe

Usage

1
recode(data = NULL, var, old.value, new.value)

Arguments

data

data frame

var

name of a new variable

old.value

vector

new.value

vector (length of 1 or same with old.value)

Details

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

Author(s)

Myo Minn Oo (Email: dr.myominnoo@gmail.com | Website: https://myominnoo.github.io/)

See Also

generate

Examples

 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)

myominnoo/mStats_beta documentation built on Feb. 29, 2020, 8:17 a.m.