- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 remove repeated column names in a data.table object in R?
In data analysis, we sometimes deal with duplicated data or just the representation of the data with same name. One such situation is column names are same for two columns in a data.table object. For this purpose, we can make use of which function with the combination of duplicated function and set the output of that duplicate to NULL to remove the repeated column names.
Example1
Loading data.table package and creating a data.table object −
library(data.table) x1<−rnorm(20) DT1<−data.table(x1,x1) DT1
Output
x1 x1 1: −1.65034927 −1.65034927 2: −1.95441645 −1.95441645 3: 2.03530252 2.03530252 4: −2.07789754 −2.07789754 5: −1.31558491 −1.31558491 6: 0.69256432 0.69256432 7: 1.83924420 1.83924420 8: −1.59751233 −1.59751233 9: −0.12015454 −0.12015454 10: 0.46507856 0.46507856 11: 1.00867249 1.00867249 12: 1.76181383 1.76181383 13: 0.35151845 0.35151845 14: −0.29470885 −0.29470885 15: −0.01617467 −0.01617467 16: 1.28775955 1.28775955 17: −1.80266832 −1.80266832 18: −0.70682196 −0.70682196 19: −2.07815278 −2.07815278 20: 0.43574626 0.43574626
Removing one x1 from DT1 −
DT1[,which(duplicated(names(DT1))):=NULL] DT1
Output
x1 1: −1.65034927 2: −1.95441645 3: 2.03530252 4: −2.07789754 5: −1.31558491 6: 0.69256432 7: 1.83924420 8: −1.59751233 9: −0.12015454 10: 0.46507856 11: 1.00867249 12: 1.76181383 13: 0.35151845 14: −0.29470885 15: −0.01617467 16: 1.28775955 17: −1.80266832 18: −0.70682196 19: −2.07815278 20: 0.43574626
Example2
y1<−rpois(20,5) DT2<−data.table(y1,y1) DT2
Output
y1 y1 1: 6 6 2: 5 5 3: 7 7 4: 5 5 5: 2 2 6: 3 3 7: 6 6 8: 5 5 9: 5 5 10: 3 3 11: 3 3 12: 4 4 13: 3 3 14: 6 6 15: 4 4 16: 5 5 17: 4 4 18: 3 3 19: 1 1 20: 2 2
Removing one y1 from DT2 −
DT2[,which(duplicated(names(DT2))):=NULL] DT2
Output
y1 1: 6 2: 5 3: 7 4: 5 5: 2 6: 3 7: 6 8: 5 9: 5 10: 3 11: 3 12: 4 13: 3 14: 6 15: 4 16: 5 17: 4 18: 3 19: 1 20: 2
- Related Articles
- How to remove dollar sign from a column of a data.table object in R?
- How to add a column to data.table object in R?
- How to change the repeated row names and column names to a sequence in a matrix in R?
- How to remove line numbers from data.table object in R?
- How to remove only first row from a data.table object in R?
- How to deal with missing column for row names when converting data frame to data.table object in R?
- How to remove the row names or column names from a matrix in R?
- How to standardize data.table object column by group in R?
- How to remove repeated numbers in sequence in R data frame column?
- How to find the mean of a column summarized by other column names and common values in those columns in data.table object in R?
- How to remove rows in a data.table object with NA's in R?
- How to remove rows in data.table object in R that contains a specific number?
- How to remove continuously repeated duplicates in an R data frame column?
- How to separate two values in single column in data.table object in R?
- How to create a correlation matrix by a categorical column in data.table object in R?

Advertisements