How to split a factor variable into n number of variables equal to factor size with full length in R data frame?


To split a factor variable into n number of variables equal to factor size with full length in R data frame, we can follow the below steps −

  • First of all, create a data frame.

  • Then, use mtabulate function of qdapTools package to split the factor variable.

Example

Create the data frame

Let’s create a data frame as shown below −

factor<-factor(sample(LETTERS[1:4],25,replace=TRUE))
df<-data.frame(factor)
df

Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

  factor
1  C
2  C
3  A
4  D
5  A
6  D
7  A
8  D
9  C
10 B
11 C
12 B
13 C
14 A
15 B
16 D
17 C
18 D
19 C
20 A
21 B
22 C
23 B
24 B
25 A

Split the factor variable

Using mtabulate function of qdapTools package to split the factor variable in data frame df −

factor<-factor(sample(LETTERS[1:4],25,replace=TRUE))
df<-data.frame(factor)
library(qdapTools)
mtabulate(df$factor)

Output

   A B C D
1  0 0 0 1
2  0 0 1 0
3  1 0 0 0
4  0 1 0 0
5  0 1 0 0
6  1 0 0 0
7  0 1 0 0
8  1 0 0 0
9  1 0 0 0
10 0 1 0 0
11 0 0 1 0
12 0 0 1 0
13 1 0 0 0
14 0 1 0 0
15 0 0 0 1
16 0 0 1 0
17 0 0 0 1
18 0 0 1 0
19 0 1 0 0
20 0 0 1 0
21 0 1 0 0
22 0 0 1 0
23 1 0 0 0
24 0 0 0 1
25 1 0 0 0

Updated on: 11-Nov-2021

344 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements