Remove duplicated columns in matrix

后端 未结 2 637
离开以前
离开以前 2021-01-05 13:27

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

相关标签:
2条回答
  • 2021-01-05 14:05

    try duplicated function on transpose of the matrix.

    duplicated.columns <- duplicated(t(your.matrix))
    
    new.matrix <- your.matrix[, !duplicated.columns]
    
    0 讨论(0)
  • 2021-01-05 14:05

    One line answer

    B = matrix(c(1, 4, 0, 2, 56, 7, 1, 4, 0, 33, 2, 5), nrow = 3)
    colnames(B) <- c("a", "b", "c", "d")
    
    B
    ##      a  b c  d
    ## [1,] 1  2 1 33
    ## [2,] 4 56 4  2
    ## [3,] 0  7 0  5
    
    B[, !duplicated(t(B))]
    ##      a  b  d
    ## [1,] 1  2 33
    ## [2,] 4 56  2
    ## [3,] 0  7  5
    
    0 讨论(0)
提交回复
热议问题