recode()
The recode()
function replaces an initial set of values with a new set of values for a vector. The function recode_all()
applies recode()
to all columns in a dataset.
The inputs for recode()
are the following:
- a vector;
- a set of values to replace (“initial values”); and
- a set of values values that will replace the initial values.
## [1] 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1
recode(mtcars$am, 0:1, 2:3)
## [1] 3 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 2 2 2 2 2 3 3 3 3 3 3 3
NAvl()
The NAvl()
function replaces a vector’s missing values with a specified value set. The function NAvl0
replaces a vector’s missing values with 0.
with(airquality, NAvl(Ozone, mean(Ozone, na.rm = TRUE)))
## [1] 41.00000 36.00000 12.00000 18.00000 42.12931 28.00000 23.00000
## [8] 19.00000 8.00000 42.12931 7.00000 16.00000 11.00000 14.00000
## [15] 18.00000 14.00000 34.00000 6.00000 30.00000 11.00000 1.00000
## [22] 11.00000 4.00000 32.00000 42.12931 42.12931 42.12931 23.00000
## [29] 45.00000 115.00000 37.00000 42.12931 42.12931 42.12931 42.12931
## [36] 42.12931 42.12931 29.00000 42.12931 71.00000 39.00000 42.12931
## [43] 42.12931 23.00000 42.12931 42.12931 21.00000 37.00000 20.00000
## [50] 12.00000 13.00000 42.12931 42.12931 42.12931 42.12931 42.12931
## [57] 42.12931 42.12931 42.12931 42.12931 42.12931 135.00000 49.00000
## [64] 32.00000 42.12931 64.00000 40.00000 77.00000 97.00000 97.00000
## [71] 85.00000 42.12931 10.00000 27.00000 42.12931 7.00000 48.00000
## [78] 35.00000 61.00000 79.00000 63.00000 16.00000 42.12931 42.12931
## [85] 80.00000 108.00000 20.00000 52.00000 82.00000 50.00000 64.00000
## [92] 59.00000 39.00000 9.00000 16.00000 78.00000 35.00000 66.00000
## [99] 122.00000 89.00000 110.00000 42.12931 42.12931 44.00000 28.00000
## [106] 65.00000 42.12931 22.00000 59.00000 23.00000 31.00000 44.00000
## [113] 21.00000 9.00000 42.12931 45.00000 168.00000 73.00000 42.12931
## [120] 76.00000 118.00000 84.00000 85.00000 96.00000 78.00000 73.00000
## [127] 91.00000 47.00000 32.00000 20.00000 23.00000 21.00000 24.00000
## [134] 44.00000 21.00000 28.00000 9.00000 13.00000 46.00000 18.00000
## [141] 13.00000 24.00000 16.00000 13.00000 23.00000 36.00000 7.00000
## [148] 14.00000 30.00000 42.12931 14.00000 18.00000 20.00000
## [1] 41 36 12 18 0 28 23 19 8 0 7 16 11 14 18 14 34 6
## [19] 30 11 1 11 4 32 0 0 0 23 45 115 37 0 0 0 0 0
## [37] 0 29 0 71 39 0 0 23 0 0 21 37 20 12 13 0 0 0
## [55] 0 0 0 0 0 0 0 135 49 32 0 64 40 77 97 97 85 0
## [73] 10 27 0 7 48 35 61 79 63 16 0 0 80 108 20 52 82 50
## [91] 64 59 39 9 16 78 35 66 122 89 110 0 0 44 28 65 0 22
## [109] 59 23 31 44 21 9 0 45 168 73 0 76 118 84 85 96 78 73
## [127] 91 47 32 20 23 21 24 44 21 28 9 13 46 18 13 24 16 13
## [145] 23 36 7 14 30 0 14 18 20
switchv()
The switchv()
function behaves the same as switch()
except that it applies over a vector (i.e., it is a vectorized version of the latter). The function swap
is a shorthand synonym for switchv()
. The function switchv_all()
/swap_all()
applies switchv()
/swap()
to all columns in a data frame.
These functions are inspired by SPSS’s RECODE
command.
switchv(iris$Species, setosa = 4, versicolor = 5, virginica = 6)
## [1] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
## [38] 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
## [75] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6
## [112] 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
## [149] 6 6