How to create a dummy variable in R?

R ProgrammingServer Side ProgrammingProgramming

A dummy variable is a type of variable that takes a value of 1 if the value for which the dummy variable is created exists in the data frame, otherwise it takes 0. Therefore, if we have a one binary variable in a data frame then there will be two dummy variables for the same. To create a dummy variable, we can use model.matrix function as shown in the below examples.

Consider the below data frame −

Example

 Live Demo

Temp<-sample(c("Hot","Cold"),20,replace=TRUE)
df1<-data.frame(Temp)
df1

Output

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

Creating dummy variable for Temp in df1 −

Example

model.matrix(~Temp-1,data=df1)

Output

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

attr(,"assign")

[1] 1 1
attr(,"contrasts")
attr(,"contrasts")$Temp
[1] "contr.treatment"

Example

 Live Demo

Group<-sample(c("A","B","C"),20,replace=TRUE)
df2<-data.frame(Group)
df2

Output

   Group
1  B
2  C
3  B
4  A
5  A
6  C
7  C
8  C
9  C
10 A
11 B
12 A
13 A
14 C
15 B
16 B
17 C
18 A
19 C
20 A

Creating dummy variable for Group in df2 −

Example

model.matrix(~Group-1,data=df2)

Output

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

attr(,"assign")

[1] 1 1 1
attr(,"contrasts")
attr(,"contrasts")$Group
[1] "contr.treatment"
raja
Published on 10-Feb-2021 06:46:49
Advertisements