When using a dataset, sometimes I want to convert a column of two values (such as Gender) to numerical values like 0, 1.

Let’s take this dataset.. It’s called “Mall_Customers” and it has a column called “Gender.” Gender has values like “Female” and “Male.”

To subset the column from the dataset use this:

> gender = Mall_Customers['Gender']

“gender” is now the values from the Gender column. However the output is still Male and Female:

> gender
1     Male
2     Male
3   Female
4   Female

This list can be converted to boolean numerics of 0 and 1, by the following:

> gender_bool <- (gender=="Female")*1

Which now outputs numeric results:

> gender_bool
  [1,]      0
  [2,]      0
  [3,]      1
  [4,]      1
  [5,]      1


No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *