12

I'm trying to identify all rows which are represented twice or more in a matrix.

For example:

m <- matrix(c(1,2,1,3,1,4,1,2,2,3,2,3,1,2,5), ncol = 3)
m
duplicated(m[,1])

Outputs:

     [,1] [,2] [,3]
[1,]    1    4    2
[2,]    2    1    3
[3,]    1    2    1
[4,]    3    2    2
[5,]    1    3    5

[1] FALSE FALSE  TRUE FALSE  TRUE

However, I do not want that output. I want:

[1] TRUE FALSE TRUE FALSE TRUE

since row[1,1]'s value appears 3 times in m's column 1.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
jeffRey
  • 123
  • 1
  • 4

2 Answers2

18

When I saw this question I asked myself "what would Jim Holtman or Bill Dunlap advise on Rhelp?". Haven't looked in the archives, but I think they might have advised using two "parallel" applications of duplicated, one with the defaults and one with the fromLast parameter and conjoining with a vector OR (|) operator.

duplicated(m[,1]) | duplicated(m[,1], fromLast=TRUE)
[1]  TRUE FALSE  TRUE FALSE  TRUE
Henrik
  • 65,555
  • 14
  • 143
  • 159
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • 4
    This should be a parameter of `duplicated()` lest we end up with such a convoluted command. – Waldir Leoncio Feb 18 '14 at 22:16
  • 1
    I'm not sure this is teh right place to ask for modification of the language. Another option might be `m[,1] %in% duplicated(m[,1])`. Does that seem more natural to you? – IRTFM Feb 19 '14 at 02:23
3

Here's one approach of many:

m <- matrix(c(1,2,1,3,1,4,1,2,2,3,2,3,1,2,5), ncol = 3)

x <- table(m[,1])
as.character(m[,1]) %in% names(x)[x > 1]

## > as.character(m[,1]) %in% names(x)[x > 1]
## [1]  TRUE FALSE  TRUE FALSE  TRUE

# or wrap it up as function:

FUN <- function(vec) {
    x <- table(vec)
    as.character(vec) %in% names(x)[x > 1]
}

FUN(m[, 1])

## > FUN(m[, 1])
## [1]  TRUE FALSE  TRUE FALSE  TRUE
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • Is there a reason for the call to `sort`? – Dason Apr 28 '13 at 18:04
  • Yes I started off by using `rle` and decided against that. So yes but no longer. I deleted it out of the response. – Tyler Rinker Apr 28 '13 at 18:34
  • Thanks for the help. I tried both out and they both worked successfully. – jeffRey Apr 28 '13 at 19:07
  • @user2329658 if they both worked perfectly don't be afraid to upvote good answers and *give* something back to the community! (if you want) :-) – Simon O'Hanlon Apr 28 '13 at 19:20
  • @SimonO101. Thanks for the tips. I just tried upvoting, I think I need to get some street cred in the form of reputation points first. – jeffRey Apr 28 '13 at 19:28
  • @user2329658 you are right - you need 15 rep. I have sent you on your way by upvoting your question, because you provided a nice reproducible example. Welcome to SO! :-) – Simon O'Hanlon Apr 28 '13 at 19:32