Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove duplicated columns in matrix

Tags:

r

I have a data set of dimension 401*5677. Among the column of this matrix there are columns which are identical but under different column names. Now, I want to keep only one column from the columns which are repeated more than once, and also get the index j for the columns removed.

Let us use as an example matrix, the following:

B=matrix(c(1,4,0,2,56,7,1,4,0,33,2,5), nrow=3)
colnames(B)<-c("a","b","c","d")

What I did so far (on my real matrix G) is:

corrG<-cor(G) 
Gtest=G
for (i in 1:nrow(corrG)){
  for (j in 1:ncol(corrG)){
    if (i<j && corrG[i,j]==1){ 
      Gtest[,j]=NA
    }
  }
}
Gfinal<-Gtest[,complete.cases(t(Gtest))] 

My code returns a matrix that still contains (!) some duplicated columns. Any help?

like image 376
Danai C. Avatar asked Apr 09 '13 14:04

Danai C.


People also ask

How do you remove duplicates from a matrix?

Method : Using loop This task can be performed in brute force manner using loops. In this, we just iterate the list of list using loop and check for the already presence of element, and append in case it's new element, and construct a non-duplicate matrix.

How do I find duplicates in columns?

columns. duplicated() returns a boolean array: a True or False for each column. If it is False then the column name is unique up to that point, if it is True then the column name is duplicated earlier. For example, using the given example, the returned value would be [False,False,True] .

How do I remove duplicate columns in R?

Example 4: Delete Duplicates in R using dplyr's distinct() Function. In the code example above, we used the function distinct() to keep only unique/distinct rows from the data frame. When working with the distinct() function, if there are duplicate rows, only the first row, of the identical ones, is preserved.


1 Answers

try duplicated function on transpose of the matrix.

duplicated.columns <- duplicated(t(your.matrix))

new.matrix <- your.matrix[, !duplicated.columns]
like image 133
Nishanth Avatar answered Oct 12 '22 23:10

Nishanth