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

问题描述 投票:13回答:4

我有非常大的矩阵,我知道,他们中的一些colnames是重复的。所以我只是想找到那些重复colnames和重复列上删除。我试过duplicate(),但它消除了重复的条目。会有人帮我R中implment呢?的一点是,重复colnames,可能不会有重复的entires。

r
4个回答
38
投票

比方说,temp是你的矩阵

temp <- matrix(seq_len(15), 5, 3)
colnames(temp) <- c("A", "A", "B")

##      A  A  B
## [1,] 1  6 11
## [2,] 2  7 12
## [3,] 3  8 13
## [4,] 4  9 14
## [5,] 5 10 15

你可以这样做

temp <- temp[, !duplicated(colnames(temp))]

##      A  B
## [1,] 1 11
## [2,] 2 12
## [3,] 3 13
## [4,] 4 14
## [5,] 5 15

或者,如果你想保持过去的重复列,你可以做

temp <- temp[, !duplicated(colnames(temp), fromLast = TRUE)] 

##       A  B
## [1,]  6 11
## [2,]  7 12
## [3,]  8 13
## [4,]  9 14
## [5,] 10 15

12
投票

或承担data.frames你可以使用subset

subset(iris, select=which(!duplicated(names(.)))) 

请注意,因为它需要在输入数据列的唯一性已经是dplyr::select在这里不适用。


1
投票

储存所有的重复到一个载体中说重复,使用-duplicates单支架子集删除重复列。

       # Define vector of duplicate cols (don't change)
       duplicates <- c(4, 6, 11, 13, 15, 17, 18, 20, 22, 
            24, 25, 28, 32, 34, 36, 38, 40, 
            44, 46, 48, 51, 54, 65, 158)

      # Remove duplicates from food and assign it to food2
         food2 <- food[,-duplicates]

0
投票

要删除某个名字的重复列,你可以做到以下几点:

test = cbind(iris, iris) # example with multiple duplicate columns
idx = which(duplicated(names(test)) & names(test) == "Species")
test = test[,-idx]

要删除所有重复列,这是一个有点简单:

test = cbind(iris, iris) # example with multiple duplicate columns
idx = which(duplicated(names(test)))
test = test[,-idx]

要么:

test = cbind(iris, iris) # example with multiple duplicate columns
test = test[,!duplicated(names(test))]
© www.soinside.com 2019 - 2024. All rights reserved.