Gin_Salmon Gin_Salmon - 2 months ago 4
R Question

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!

Answer

Try this:

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