1

Data:

 df <- data.frame("y"= c(rep(100,10), rep(103,10)), "x" = c(rep(1,8),2,2,rep(4,5),7,7, rep(4,3)) )

For each value of y, we expect a unique value of x. But in data, we have a couple of unwanted values. Based on mode, we can find the corresponding value of x for each value of y.

Can we identify the row numbers where there is unwanted pairing ?

Expected output: 9, 10, 16, 17

zx8754
  • 52,746
  • 12
  • 114
  • 209
Navin Manaswi
  • 964
  • 7
  • 19

1 Answers1

3

We can use ave to group by 'y', get the logical index of elements in 'x' that are not the 'Mode', from there get the row index with which.

with(df, which(as.logical(ave(x, y, FUN= function(x) x!=Mode(x)))))
#[1]  9 10 16 17

Or as @thelatemail mentioned in the comments, the above can be made compact

which(with(df, x != ave(x, y, FUN=Mode) )) 

where Mode is (taken from here)

Mode <- function(x) {
  ux <- unique(x)
 ux[which.max(tabulate(match(x, ux)))]
}
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662