0

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.

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
manoj1123
  • 125
  • 1
  • 1
  • 7
  • 2
    Factors are coded as consecutive integers *starting at `1`*. When you use `as.integer` or `as.numeric` directly on the factor, you are getting *those* integers. When you use the former, you are converting the *levels* to numeric, then subsetting with the consecutive integers, the factor values. – Rui Barradas Jul 08 '20 at 17:44
  • Tip: break `as.numeric(levels(student))[student]` in pieces, first run `as.numeric(levels(student))` then the full instruction. – Rui Barradas Jul 08 '20 at 17:46
  • Thanks. I forgot to break and execute. This helps! – manoj1123 Jul 08 '20 at 17:48

0 Answers0