Using ISLR library and the data set.
library(tidyverse)
library(ISLR)
data("Default")
Replacing "No" with 0 and "Yes" with 1 for variables default and student
DF1 <- Default %>% 
  select(default, student) %>% 
  mutate(default = factor(default, labels = c("No" = 1, "Yes" = 10)),
         student = factor(student, labels = c("No" = 1, "Yes" = 10)))
converting the variables to numeric
DF2 <- DF1 %>% 
  mutate(default = as.integer(default),
         student = as.numeric(student))
The output is as shown below.
> head(DF2)  
  default student
1       1       1
2       1       2
3       1       1
4       1       1
5       1       1
6       1       2
While converting to numeric type those 0, 1 values are coded as 1,2 etc.
I got SO answer here. How to convert a factor to integer\numeric without loss of information?
DF2 <- DF1 %>% 
  mutate(default = as.integer(levels(default))[default],
         student = as.numeric(levels(student))[student])
> head(DF2)  
  default student
1       0       0
2       0       1
3       0       0
4       0       0
5       0       0
6       0       1
Can you please explain a little on the statement
as.numeric(levels(student))[student])  
Why doesn't the instruction below give the desired output?
as.numeric(as.factor(student))
Thanks.
 
    