Home > Enterprise >  How to change gender factor value to 0 and 1 instead of 1 and 2 for my excel dataset in r
How to change gender factor value to 0 and 1 instead of 1 and 2 for my excel dataset in r

Time:10-11

This is my code but everytime i view it, it still me that female is 1 and male is 2. What is wrong with my coding?

class(Dataset_A$gender)
Gender_numeric <- as.numeric(factor(Dataset_A$gender,levels=c("female","male"),labels=c("0","1")))
class(Gender_numeric)
View(Gender_numeric)

CodePudding user response:

The main issue is that factor to direct conversion to numeric/integer returns the integer storage mode values which start from 1. Instead, we need to use as.numeric on the character converted object which is mentioned in the documentation (?factor)

In particular, as.numeric applied to a factor is meaningless, and may happen by implicit coercion. To transform a factor f to approximately its original numeric values, as.numeric(levels(f))[f] is recommended and slightly more efficient than as.numeric(as.character(f)).

Gender_numeric <- as.numeric(as.character(factor(Dataset_A$gender,
          levels=c("female",
       "male"),labels=c("0","1"))))

With a small example

v1 <- c("male", "female", "male", "female")
v2 <- factor(v1, levels = c("female", "male"), labels = c("0", "1"))

If we look at 'v2', the new levels are 0 and 1

> v2
[1] 1 0 1 0
Levels: 0 1

But , converting to integer/numeric directly

> as.integer(v2)
[1] 2 1 2 1

instead, do convert to character first`

> as.integer(as.character(v2))
[1] 1 0 1 0

Or may also do this using levels (which would be fast)

> as.integer(levels(v2)[v2])
[1] 1 0 1 0
  •  Tags:  
  • r
  • Related