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

Updated on: 05-Feb-2021

277 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements