How to replace words in a dichotomous column for an R data frame with numbers?


A dichotomous column can be represented by words such as Yes/No, Good/Bad, Right/Wrong etc. To replace such words with number, say 1 and 0 we can use ifelse function.

For example, if we have a data frame called df that contains a column Binary with Yes and No values then we can replace Yes with 1 and No with 0 by using the below command −

df$Binary<-ifelse(df$Binary=="Yes",1,0)

Example 1

Following snippet creates a sample data frame −

x<-sample(c("Yes","No"),20,replace=TRUE)
df1<-data.frame(x)
df1

The following dataframe is created −

    x
1  Yes
2  Yes
3  Yes
4  Yes
5   No
6  Yes
7  Yes
8  Yes
9  Yes
10 Yes
11 Yes
12  No
13 Yes
14  No
15  No
16 Yes
17  No
18 Yes
19 Yes
20 Yes

To replace words Yes with 1 and No with 0, add the following code to the above snippet −

x<-sample(c("Yes","No"),20,replace=TRUE)
df1<-data.frame(x)
df1$x<-ifelse(df1$x=="Yes",1,0)
df1

Output

If you execute all the above given snippets as a single program, it generates the following output −

   x
1  1
2  1
3  1
4  1
5  0
6  1
7  1
8  1
9  1
10 1
11 1
12 0
13 1
14 0
15 0
16 1
17 0
18 1
19 1
20 1

Example 2

Following snippet creates a sample data frame −

y<-sample(c("Good","Bad"),20,replace=TRUE)
df2<-data.frame(y)
df2

The following dataframe is created −

   y
1  Bad
2  Bad
3  Bad
4  Good
5  Good
6  Good
7  Good
8  Bad
9  Bad
10 Bad
11 Bad
12 Bad
13 Good
14 Bad
15 Good
16 Good
17 Good
18 Bad
19 Good
20 Good

To replace words Good with 1 and Bad with 0, add the following code to the above snippet −

y<-sample(c("Good","Bad"),20,replace=TRUE)
df2<-data.frame(y)
df2$y<-ifelse(df2$y=="Good",1,0)
df2

Output

If you execute all the above given snippets as a single program, it generates the following output −

   y
1  0
2  0
3  0
4  1
5  1
6  1
7  1
8  0
9  0
10 0
11 0
12 0
13 1
14 0
15 1
16 1
17 1
18 0
19 1
20 1

Updated on: 09-Nov-2021

278 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements