Can you please explain what do you mean by this? Can you please provide an expected object for a copy-paste friendly sample dataset? As Mara has noted, a reprex
will be very helpful.
If you meant something like coding c("A", "B", "A", "A", "B", "C")
as c(1, 2, 1, 1, 2, 3)
, then you can use the as.integer
function. Or, you want to recode by some other labels, you can use the labels
argument of the factor
function.
Here, I'm providing an example, where I've recoded to integers but through the factor
function. I'm recoding all columns except one particular column.
set.seed(seed = 28127)
suppressPackageStartupMessages(expr = library(package = "dplyr"))
dataset <- data.frame(Class = sample(x = c("no-recurrence-events", "recurrence-events"),
size = 20,
replace = TRUE),
PostMeno = sample(x = c("It40", "premeno", "ge40"),
size = 20,
replace = TRUE),
NodeCaps = sample(x = c("no", "yes"),
size = 20,
replace = TRUE),
Breast = sample(x = c("left", "right"),
size = 20,
replace = TRUE),
Quadrant = sample(x = c("left_low", "right_up", "central", "left_up", "right_low"),
size = 20,
replace = TRUE),
Radiation = sample(x = c("no", "yes"),
size = 20,
replace = TRUE))
dataset %>%
mutate_at(.vars = vars(-Radiation),
.funs = function(y) factor(x = y,
labels = seq_len(length.out = nlevels(x = y))))
#> Class PostMeno NodeCaps Breast Quadrant Radiation
#> 1 1 3 1 1 1 yes
#> 2 1 3 1 1 1 no
#> 3 2 2 1 1 1 no
#> 4 1 2 1 2 5 yes
#> 5 1 1 1 1 5 no
#> 6 1 2 1 2 2 yes
#> 7 1 1 1 2 3 yes
#> 8 1 3 2 2 3 no
#> 9 2 2 1 2 1 no
#> 10 2 1 1 1 3 yes
#> 11 2 1 2 2 2 yes
#> 12 1 2 2 2 5 yes
#> 13 2 1 2 1 4 yes
#> 14 2 2 2 2 5 no
#> 15 2 2 2 2 1 yes
#> 16 1 2 1 2 4 no
#> 17 1 2 2 2 5 yes
#> 18 1 1 1 2 1 no
#> 19 2 3 2 1 1 no
#> 20 1 2 1 1 1 yes
Created on 2019-04-09 by the reprex package (v0.2.1)
Hope this helps.