如何删除 R 中重复的列名?

How to remove duplicate column names in R?

我有一个大数据框,想删除重复的列

为简单起见,假设这是我的数据:

df <- data.frame(id1 = c("Aa","Aa","Ba","Ca","Da"), id2 = c(2,1,4,5,10), location=c(351,261,101,91,51), comment=c(35,26,10,9,5), comment=c(5,16,25,14,11), hight=c(15,21,5,19,18), check.names = FALSE)

我可以使用以下方法删除重复的列名“comment”:

df <- df[!duplicated(colnames(df))]

但是,当我在我的真实数据框中应用相同的代码时,它 returns 出现错误:

Error in `[.data.table`(SNV_wild, !duplicated(colnames(SNV_wild))) : 
  i evaluates to a logical vector length 1883 but there are 60483 rows. Recycling of logical i is no longer allowed as it hides more bugs than is worth the rare convenience. Explicitly use rep(...,length=.N) if you really need to recycle.

抱歉,我无法 post 真实数据,因为它非常大,您可能会看到错误。

我该如何解决这个问题 - 我已经遍历了所有列名,但有重复的列名。

提前致谢

您的真实数据框是 class data.table,而您的小示例不是。你可以试试:

df[,!duplicated(colnames(df)), with=F]