Gin_Salmon
Gin_Salmon

Reputation: 847

Removing all but one duplicated column with same name R

I've got a data table with about 200 column names in it, however, I have several columns which are repeated and are exactly the same in all respects, i.e they have the same name and same entries.

I want to get rid of all but one of these duplicated columns.

Take for instance:

Code         AEE       AEE      Code      AEE    EPI       Code     AEPI
20/09/1991  4562.43 108.13  20/09/1991  2017698 60.16   20/09/1991  18309
23/09/1991  4578.89 108.52  23/09/1991  2017698 56.55   23/09/1991  18309
24/09/1991  4578.89 108.52  24/09/1991  2017698 58.36   24/09/1991  18309
25/09/1991  4631.04 109.76  25/09/1991  2017698 56.55   25/09/1991  18309
26/09/1991  4665.34 110.57  26/09/1991  2017698 58.36   26/09/1991  18309

As you can see the Code column repeats every so often.

Doing: Data[, Code := NULL] only gets rid of the first "Code" and not the others.

Ideally the output would look like:

    Code       AEE   AEE     AEE     EPI    AEPI
20/09/1991  4562.43 108.13  2017698 60.16   18309
23/09/1991  4578.89 108.52  2017698 56.55   18309
24/09/1991  4578.89 108.52  2017698 58.36   18309
25/09/1991  4631.04 109.76  2017698 56.55   18309
26/09/1991  4665.34 110.57  2017698 58.36   18309

So only the first Code column remains. Thanks!

Upvotes: 1

Views: 122

Answers (3)

989
989

Reputation: 12935

You could also do:

df <- df[,!duplicated(names(df))]

OR

df <- df[,unique(names(df))]

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522626

Try this:

Data <- Data[, !duplicated(lapply(Data, summary))]

Upvotes: 2

akuiper
akuiper

Reputation: 215117

You can delete by the column number:

Data[, c(4,7) := NULL]   

Data
#         Code     AEE    AEE     AEE   EPI  AEPI
#1: 20/09/1991 4562.43 108.13 2017698 60.16 18309
#2: 23/09/1991 4578.89 108.52 2017698 56.55 18309
#3: 24/09/1991 4578.89 108.52 2017698 58.36 18309
#4: 25/09/1991 4631.04 109.76 2017698 56.55 18309
#5: 26/09/1991 4665.34 110.57 2017698 58.36 18309

Upvotes: 0

Related Questions