Recode discrete variables, including numerice and character variable.

rec_num(x, rec, keep = TRUE)

rec_char(x, rec, keep = TRUE)

Arguments

x

A numeric or character vector.

rec

String with recode pairs of old and new values. Find the usage in examples.

keep

Logical. Decide whether to keep the original values if not recoded. Defaults to TRUE.

Value

A vector.

See also

rec

Examples


x = 1:10
x
#>  [1]  1  2  3  4  5  6  7  8  9 10
rec_num(x, rec = "1=10; 4=2")
#>  [1] 10  2  3  2  5  6  7  8  9 10
rec_num(x, rec = "1:3=1; 4:6=2")
#>  [1]  1  1  1  2  2  2  7  8  9 10
rec_num(x, rec = "1:3=1; 4:6=2",keep = FALSE)
#>  [1]  1  1  1  2  2  2 NA NA NA NA

y = letters[1:5]
y
#> [1] "a" "b" "c" "d" "e"
rec_char(y,rec = "a=A;b=B")
#> [1] "A" "B" "c" "d" "e"
rec_char(y,rec = "a,b=A;c,d=B")
#> [1] "A" "A" "B" "B" "e"
rec_char(y,rec = "a,b=A;c,d=B",keep = FALSE)
#> [1] "A" "A" "B" "B" NA