Skip to content
Prev 377325 / 398502 Next

Identify row indices corresponding to each distinct row of a matrix

The duplicated function returns TRUE for rows that have already appeared... exactly one of the rows is not represented in the output of duplicated. For the intended purpose of removing duplicates this behavior is ideal. I have no idea what your intended purpose is, since every row has duplicates elsewhere in the matrix. If you really want every set identified this way then a loop/apply seems inevitable (most opportunities for optimization come about by not visiting every combination).

Cm <- as.matrix( C )
D <- which( !duplicated( Cm, MARGIN=1 ) )
nCm <- nrow( Cm )
F <- lapply( D, function(d) {
   idxrep <- rep( d, nCm )
   which( 0 == unname( rowSums( Cm[idxrep,] != Cm ) ) )
  } )
On November 8, 2018 1:42:40 PM PST, li li <hannah.hlx at gmail.com> wrote: