简体   繁体   English

删除所有重复的列,但重复的列名称相同R

[英]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, ie they have the same name and same entries. 我有一个包含约200个列名的数据表,但是,我有几个重复的列,各方面都完全相同,即它们具有相同的名称和相同的条目。

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. 这样做: Data[, Code := NULL]仅摆脱第一个“ Code”,而不摆脱其他“ Code”。

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! 谢谢!

尝试这个:

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

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

You could also do: 您也可以这样做:

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

OR 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM