[R] find a row identical to another

Petr Savicky savicky at cs.cas.cz
Wed Feb 8 18:06:33 CET 2012

On Wed, Feb 08, 2012 at 03:36:45PM +0100, Francisco wrote:
> Hello,
> I have a dataset with many rows, starting from a row that I choose I 
> would like to find the other rows in the dataset which are identical to 
> this row (with the same values per each column) and assign them to a 
> variable.
> How could I do?


Try the following.

  # prepare a data frame
  df1 <- expand.grid(x=1:2, y=letters[1:2])
  df <- rbind(df1, df1, df1)

  # look for later occurences of df[i, ]
  i <- 6
  ind <- unname(which(rowSums(df == df[rep(i, times=nrow(df)), ]) == ncol(df)))
  ind[ind >= i]

  [1]  6 10

Hope this helps.

See also duplicated(df). This finds all duplicates,
not only duplicates of a given row.

Petr Savicky.

More information about the R-help mailing list