Convert a numeric column to binary factor based on a condition in R data frame

R ProgrammingServer Side ProgrammingProgramming

To convert a numeric column to binary factor based on a condition in R data frame, we can use factor function along with ifelse function.

For Example, if we have a data frame called df that contains a numerical column say Num and we want to convert it to a binary factor if Num is less than 100 then it will be Minor otherwise Major then we can use the below given command −

df$Num_Factor<-factor(ifelse(df$Num_Factor<100,"Minor","Major"))

Example 1

Following snippet creates a sample data frame −

x<-sample(1:50,20)
df1<-data.frame(x)
df1

The following dataframe is created

   x
 1 4
 2 13
 3 50
 4 19
 5 43
 6 42
 7 18
 8 17
 9 27
10 23
11 31
12 37
13 5
14 28
15 1
16 6
17 30
18 35
19 22
20 10

To convert x into a factor column on the above created data frame, add the following code to the above snippet −

x<-sample(1:50,20)
df1<-data.frame(x)
df1$x_Factor<-factor(ifelse(df1$x<25,"Low","High"))
df1

Output

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

    x x_Factor
 1  4  Low
 2 13 Low
 3 50 High
 4 19 Low
 5 43 High
 6 42 High
 7 18 Low
 8 17 Low
 9 27 High
10 23 Low
11 31 High
12 37 High
13 5  Low
14 28 High
15 1  Low
16 6  Low
17 30 High
18 35 High
19 22 Low
20 10 Low

Example 2

Following snippet creates a sample data frame −

y<-round(rnorm(20,1,0.25),1)
df2<-data.frame(y)
df2

The following dataframe is created

    y
 1 1.4
 2 0.9
 3 1.0
 4 0.6
 5 1.1
 6 0.7
 7 0.9
 8 1.2
 9 0.8
10 1.1
11 0.7
12 0.4
13 1.1
14 0.8
15 1.1
16 0.7
17 1.7
18 0.8
19 1.1
20 0.6

To convert y into a factor column on the above created data frame, add the following code to the above snippet −

y<-round(rnorm(20,1,0.25),1)
df2<-data.frame(y)
df2$y_Factor<-factor(ifelse(df2$y<1,"Rejected","Accepted"))
df2

Output

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

    y  y_Factor
 1 1.4 Accepted
 2 0.9 Rejected
 3 1.0 Accepted
 4 0.6 Rejected
 5 1.1 Accepted
 6 0.7 Rejected
 7 0.9 Rejected
 8 1.2 Accepted
 9 0.8 Rejected
10 1.1 Accepted
11 0.7 Rejected
12 0.4 Rejected
13 1.1 Accepted
14 0.8 Rejected
15 1.1 Accepted
16 0.7 Rejected
17 1.7 Accepted
18 0.8 Rejected
19 1.1 Accepted
20 0.6 Rejected
raja
Published on 10-Nov-2021 08:00:27
Advertisements