How to make duplicate factor levels unique in an R data frame?

R ProgrammingServer Side ProgrammingProgramming

The factor with duplicate levels represents grouping data but if we want to convert the grouping data into nominal data then duplicate values must be removed or converted into unique values. To make duplicate factor levels unique in an R data frame, we can use make.unique function.

Check out the below Examples to understand how it works.

Example 1

Following snippet creates a sample data frame −

Factor<-factor(sample(c("Hot","Cold","Warm","Room Temp"),20,replace=TRUE))
Sales<-sample(11:50,20)
df1<-data.frame(Factor,Sales)
df1

The following dataframe is created

 Factor    Sales
1  Warm      43
2  Cold      50
3  Hot       33
4  Hot       25
5  Warm      22
6  Cold      20
7  Hot       18
8  Warm      35
9  Hot       38
10 Room Temp 32
11 Cold      41
12 Hot       40
13 Cold      21
14 Cold      15
15 Cold      23
16 Room Temp 26
17 Cold      48
18 Warm      28
19 Cold      42
20 Room Temp 27

To make levels in Factor column of df1 unique on the above created data frame, add the following code to the above snippet −

Factor<-factor(sample(c("Hot","Cold","Warm","Room Temp"),20,replace=TRUE))
Sales<-sample(11:50,20)
df1<-data.frame(Factor,Sales)
within(df1,Factor<- ave(as.character(Factor),FUN=make.unique))

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

 Factor    Sales
1 Warm       43
2 Cold       50
3 Hot        33
4 Hot.1        25
5 Warm.1       22
6 Cold.1       20
7 Hot.2        18
8 Warm.2       35
9 Hot.3        38
10 Room Temp   32
11 Cold.2      41
12 Hot.4       40
13 Cold.3      21
14 Cold.4      15
15 Cold.5      23
16 Room Temp.1 26
17 Cold.6      48
18 Warm.3      28
19 Cold.7      42
20 Room Temp.2 27

Example 2

Following snippet creates a sample data frame −

Class<-factor(sample(c("First","Second","Third"),20,replace=TRUE))
Price<-sample(1:10,20,replace=TRUE)
df2<-data.frame(Class,Price)
df2

The following dataframe is created

  Class   Price
1 First    9
2 Second   5
3 Second   7
4 First    7
5 First    8
6 Second   9
7 First    8
8 First   10
9 First    3
10 Third   3
11 Third   6
12 First   3
13 First   8
14 First   3
15 Third   5
16 Second  2
17 First   8
18 Second  2
19 Third   4
20 Third   4

To make levels in Class column of df2 unique on the above created data frame, add the following code to the above snippet −

Class<-factor(sample(c("First","Second","Third"),20,replace=TRUE))
Price<-sample(1:10,20,replace=TRUE)
df2<-data.frame(Class,Price)
within(df2,Class<-ave(as.character(Class),FUN=make.unique))

Output

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

  Class   Price
1 First     9
2 Second    5
3 Second.1  7
4 First.1   7
5 First.2   8
6 Second.2  9
7 First.3   8
8 First.4  10
9 First.5   3
10 Third    3
11 Third.1  6
12 First.6  3
13 First.7  8
14 First.8  3
15 Third.2  5
16 Second.3 2
17 First.9  8
18 Second.4 2
19 Third.3  4
20 Third.4  4
raja
Published on 02-Nov-2021 06:06:58
Advertisements