- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to make duplicate factor levels unique in an R data frame?
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
- Related Articles
- How to collapse factor levels in an R data frame?
- How to extract the factor levels from factor column in an R data frame?
- How to create scatterplot for factor levels in an R data frame?
- How to create a new level using unique levels of a factor in R data frame?
- How to make all values in an R data frame unique?
- How to combine the levels of a factor variable in an R data frame?
- How to find the cumulative sum for factor levels in an R data frame?
- How to drop factor levels in subset of a data frame in R?
- How to find the sum by distinct column for factor levels in an R data frame?
- How to convert numeric levels of a factor into string in R data frame?
- How to subset factor columns in an R data frame?
- How to find the unique rows in an R data frame?
- How to sort a numerical factor column in an R data frame?
- How to extract unique combination of rows in an R data frame?
- How to count the number of duplicate rows in an R data frame?
