
Encode categorical variables
encode_categorical.RdConverts factor or character columns into numeric representations using label encoding, frequency encoding, or one-hot encoding. One-hot encoding creates binary indicator columns for each category.
Usage
encode_categorical(data, cols = NULL, method = "label",
group = NULL, prefix = "enc_", verbose = FALSE)Arguments
- data
A data frame containing categorical variables.
- cols
The column indices or names of selected categorical variables. If NULL, all factor/character columns are used.
- method
Encoding method. One of
"label"(integer labels),"frequency"(frequency of each category), or"onehot"(binary indicator columns).- group
Optional grouping column for grouped frequency encoding.
- prefix
Prefix for one-hot encoded column names (used only when
method = "onehot").- verbose
Logical; if
TRUE, prints progress message.
Value
A data frame with encoded variables. For one-hot encoding, original categorical columns are replaced by binary columns.
Examples
# Label encoding
encode_categorical(data[1:100, c(1, 4)], cols = 2, method = "label")
#> date monthyear
#> 1 2019-12-31 16:00:00 1
#> 2 2019-12-31 16:10:00 1
#> 3 2019-12-31 16:20:00 1
#> 4 2019-12-31 16:30:00 1
#> 5 2019-12-31 16:40:00 1
#> 6 2019-12-31 16:50:00 1
#> 7 2019-12-31 17:00:00 1
#> 8 2019-12-31 17:10:00 1
#> 9 2019-12-31 17:20:00 1
#> 10 2019-12-31 17:30:00 1
#> 11 2019-12-31 17:40:00 1
#> 12 2019-12-31 17:50:00 1
#> 13 2019-12-31 18:00:00 1
#> 14 2019-12-31 18:10:00 1
#> 15 2019-12-31 18:20:00 1
#> 16 2019-12-31 18:30:00 1
#> 17 2019-12-31 18:40:00 1
#> 18 2019-12-31 18:50:00 1
#> 19 2019-12-31 19:00:00 1
#> 20 2019-12-31 19:10:00 1
#> 21 2019-12-31 19:20:00 1
#> 22 2019-12-31 19:40:00 1
#> 23 2019-12-31 19:50:00 1
#> 24 2019-12-31 20:00:00 1
#> 25 2019-12-31 20:10:00 1
#> 26 2019-12-31 20:20:00 1
#> 27 2019-12-31 20:30:00 1
#> 28 2019-12-31 20:40:00 1
#> 29 2019-12-31 20:50:00 1
#> 30 2019-12-31 21:00:00 1
#> 31 2019-12-31 21:10:00 1
#> 32 2019-12-31 21:20:00 1
#> 33 2019-12-31 21:30:00 1
#> 34 2019-12-31 21:40:00 1
#> 35 2019-12-31 21:50:00 1
#> 36 2019-12-31 22:00:00 1
#> 37 2019-12-31 22:10:00 1
#> 38 2019-12-31 22:20:00 1
#> 39 2019-12-31 22:30:00 1
#> 40 2019-12-31 22:40:00 1
#> 41 2019-12-31 22:50:00 1
#> 42 2019-12-31 23:00:00 1
#> 43 2019-12-31 23:10:00 1
#> 44 2019-12-31 23:20:00 1
#> 45 2019-12-31 23:30:00 1
#> 46 2019-12-31 23:40:00 1
#> 47 2019-12-31 23:50:00 1
#> 48 2020-01-01 00:00:00 1
#> 49 2020-01-01 00:10:00 1
#> 50 2020-01-01 00:20:00 1
#> 51 2020-01-01 00:30:00 1
#> 52 2020-01-01 00:40:00 1
#> 53 2020-01-01 00:50:00 1
#> 54 2020-01-01 01:00:00 1
#> 55 2020-01-01 01:10:00 1
#> 56 2020-01-01 01:20:00 1
#> 57 2020-01-01 01:30:00 1
#> 58 2020-01-01 01:40:00 1
#> 59 2020-01-01 01:50:00 1
#> 60 2020-01-01 02:20:00 1
#> 61 2020-01-01 02:30:00 1
#> 62 2020-01-01 02:40:00 1
#> 63 2020-01-01 02:50:00 1
#> 64 2020-01-01 03:00:00 1
#> 65 2020-01-01 03:10:00 1
#> 66 2020-01-01 03:20:00 1
#> 67 2020-01-01 03:30:00 1
#> 68 2020-01-01 03:40:00 1
#> 69 2020-01-01 03:50:00 1
#> 70 2020-01-01 04:00:00 1
#> 71 2020-01-01 04:10:00 1
#> 72 2020-01-01 04:20:00 1
#> 73 2020-01-01 04:30:00 1
#> 74 2020-01-01 04:40:00 1
#> 75 2020-01-01 04:50:00 1
#> 76 2020-01-01 05:00:00 1
#> 77 2020-01-01 05:40:00 1
#> 78 2020-01-01 05:50:00 1
#> 79 2020-01-01 06:00:00 1
#> 80 2020-01-01 06:10:00 1
#> 81 2020-01-01 06:20:00 1
#> 82 2020-01-01 06:30:00 1
#> 83 2020-01-01 06:40:00 1
#> 84 2020-01-01 06:50:00 1
#> 85 2020-01-01 07:00:00 1
#> 86 2020-01-01 07:10:00 1
#> 87 2020-01-01 07:20:00 1
#> 88 2020-01-01 07:30:00 1
#> 89 2020-01-01 07:40:00 1
#> 90 2020-01-01 07:50:00 1
#> 91 2020-01-01 08:00:00 1
#> 92 2020-01-01 08:10:00 1
#> 93 2020-01-01 08:20:00 1
#> 94 2020-01-01 08:30:00 1
#> 95 2020-01-01 08:40:00 1
#> 96 2020-01-01 08:50:00 1
#> 97 2020-01-01 09:00:00 1
#> 98 2020-01-01 09:10:00 1
#> 99 2020-01-01 09:20:00 1
#> 100 2020-01-01 09:30:00 1
# One-hot encoding
encode_categorical(data[1:100, c(1, 4)], cols = 2, method = "onehot")
#> date enc_monthyear_January.2020
#> 1 2019-12-31 16:00:00 1
#> 2 2019-12-31 16:10:00 1
#> 3 2019-12-31 16:20:00 1
#> 4 2019-12-31 16:30:00 1
#> 5 2019-12-31 16:40:00 1
#> 6 2019-12-31 16:50:00 1
#> 7 2019-12-31 17:00:00 1
#> 8 2019-12-31 17:10:00 1
#> 9 2019-12-31 17:20:00 1
#> 10 2019-12-31 17:30:00 1
#> 11 2019-12-31 17:40:00 1
#> 12 2019-12-31 17:50:00 1
#> 13 2019-12-31 18:00:00 1
#> 14 2019-12-31 18:10:00 1
#> 15 2019-12-31 18:20:00 1
#> 16 2019-12-31 18:30:00 1
#> 17 2019-12-31 18:40:00 1
#> 18 2019-12-31 18:50:00 1
#> 19 2019-12-31 19:00:00 1
#> 20 2019-12-31 19:10:00 1
#> 21 2019-12-31 19:20:00 1
#> 22 2019-12-31 19:40:00 1
#> 23 2019-12-31 19:50:00 1
#> 24 2019-12-31 20:00:00 1
#> 25 2019-12-31 20:10:00 1
#> 26 2019-12-31 20:20:00 1
#> 27 2019-12-31 20:30:00 1
#> 28 2019-12-31 20:40:00 1
#> 29 2019-12-31 20:50:00 1
#> 30 2019-12-31 21:00:00 1
#> 31 2019-12-31 21:10:00 1
#> 32 2019-12-31 21:20:00 1
#> 33 2019-12-31 21:30:00 1
#> 34 2019-12-31 21:40:00 1
#> 35 2019-12-31 21:50:00 1
#> 36 2019-12-31 22:00:00 1
#> 37 2019-12-31 22:10:00 1
#> 38 2019-12-31 22:20:00 1
#> 39 2019-12-31 22:30:00 1
#> 40 2019-12-31 22:40:00 1
#> 41 2019-12-31 22:50:00 1
#> 42 2019-12-31 23:00:00 1
#> 43 2019-12-31 23:10:00 1
#> 44 2019-12-31 23:20:00 1
#> 45 2019-12-31 23:30:00 1
#> 46 2019-12-31 23:40:00 1
#> 47 2019-12-31 23:50:00 1
#> 48 2020-01-01 00:00:00 1
#> 49 2020-01-01 00:10:00 1
#> 50 2020-01-01 00:20:00 1
#> 51 2020-01-01 00:30:00 1
#> 52 2020-01-01 00:40:00 1
#> 53 2020-01-01 00:50:00 1
#> 54 2020-01-01 01:00:00 1
#> 55 2020-01-01 01:10:00 1
#> 56 2020-01-01 01:20:00 1
#> 57 2020-01-01 01:30:00 1
#> 58 2020-01-01 01:40:00 1
#> 59 2020-01-01 01:50:00 1
#> 60 2020-01-01 02:20:00 1
#> 61 2020-01-01 02:30:00 1
#> 62 2020-01-01 02:40:00 1
#> 63 2020-01-01 02:50:00 1
#> 64 2020-01-01 03:00:00 1
#> 65 2020-01-01 03:10:00 1
#> 66 2020-01-01 03:20:00 1
#> 67 2020-01-01 03:30:00 1
#> 68 2020-01-01 03:40:00 1
#> 69 2020-01-01 03:50:00 1
#> 70 2020-01-01 04:00:00 1
#> 71 2020-01-01 04:10:00 1
#> 72 2020-01-01 04:20:00 1
#> 73 2020-01-01 04:30:00 1
#> 74 2020-01-01 04:40:00 1
#> 75 2020-01-01 04:50:00 1
#> 76 2020-01-01 05:00:00 1
#> 77 2020-01-01 05:40:00 1
#> 78 2020-01-01 05:50:00 1
#> 79 2020-01-01 06:00:00 1
#> 80 2020-01-01 06:10:00 1
#> 81 2020-01-01 06:20:00 1
#> 82 2020-01-01 06:30:00 1
#> 83 2020-01-01 06:40:00 1
#> 84 2020-01-01 06:50:00 1
#> 85 2020-01-01 07:00:00 1
#> 86 2020-01-01 07:10:00 1
#> 87 2020-01-01 07:20:00 1
#> 88 2020-01-01 07:30:00 1
#> 89 2020-01-01 07:40:00 1
#> 90 2020-01-01 07:50:00 1
#> 91 2020-01-01 08:00:00 1
#> 92 2020-01-01 08:10:00 1
#> 93 2020-01-01 08:20:00 1
#> 94 2020-01-01 08:30:00 1
#> 95 2020-01-01 08:40:00 1
#> 96 2020-01-01 08:50:00 1
#> 97 2020-01-01 09:00:00 1
#> 98 2020-01-01 09:10:00 1
#> 99 2020-01-01 09:20:00 1
#> 100 2020-01-01 09:30:00 1