Convert Data Type of All Columns from Integer to Factor in R



To convert the data type of all columns from integer to factor, we can use lapply function with factor function.

For example, if we have a data frame called df which has all integer columns then we can use the below given command to convert all columns data type to factor −

df<-data.frame(lapply(df,factor))

Example 1

Following snippet creates a sample data frame −

x1<-rpois(20,1)
x2<-rpois(20,2)
df1<-data.frame(x1,x2)
df1

The following dataframe is created −

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

To convert the data type of all columns from integer to factor, add the following code to the above snippet −

x1<-rpois(20,1)
x2<-rpois(20,2)
df1<-data.frame(x1,x2)
str(df1)

Output

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

'data.frame': 20 obs. of 2 variables:
$ x1: int 1 3 0 1 1 1 1 3 2 3 ...
$ x2: int 4 0 1 1 2 1 2 2 3 5 ...

To convert data type of columns in df1 to factor, add the following code to the above snippet −

x1<-rpois(20,1)
x2<-rpois(20,2)
df1<-data.frame(x1,x2)
df1<-data.frame(lapply(df1,factor))
str(df1)

Outpu

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

'data.frame': 20 obs. of 2 variables:
$ x1: Factor w/ 4 levels "0","1","2","3": 2 4 1 2 2 2 2 4 3 4 ...
$ x2: Factor w/ 6 levels "0","1","2","3",..: 5 1 2 2 3 2 3 3 4 6 ...

Example 2

Following snippet creates a sample data frame −

y1<-sample(0:1,20,replace=TRUE)
y2<-sample(1:4,20,replace=TRUE)
df2<-data.frame(y1,y2)
df2

The following dataframe is created −

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

To convert the data type of all columns from integer to factor, add the following code to the above snippet −

y1<-sample(0:1,20,replace=TRUE)
y2<-sample(1:4,20,replace=TRUE)
df2<-data.frame(y1,y2)
str(df2)

Output

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

'data.frame': 20 obs. of 2 variables:
$ y1: int 0 1 0 0 1 0 0 1 0 0 ...
$ y2: int 2 3 2 2 3 3 2 3 4 1 ...

To convert data type of columns in df2 to factor, add the following code to the above snippet −

y1<-sample(0:1,20,replace=TRUE)
y2<-sample(1:4,20,replace=TRUE)
df2<-data.frame(y1,y2)
df2<-data.frame(lapply(df2,factor))
str(df2)

Output

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

'data.frame': 20 obs. of 2 variables:
$ y1: Factor w/ 2 levels "0","1": 1 2 1 1 2 1 1 2 1 1 ...
$ y2: Factor w/ 4 levels "1","2","3","4": 2 3 2 2 3 3 2 3 4 1 ...
Updated on: 2021-11-12T07:39:37+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements