user3067923
user3067923

Reputation: 447

reporting identical values across columns in matrix

I have a matrix that I am performing a for loop over. I want to know if the values of position i in the for loop exist anywhere else in the matrix, and if so, report TRUE. The matrix looks like this

dim
     x y
[1,] 5 1
[2,] 2 2
[3,] 5 1
[4,] 5 9

In this case, dim[1,] is the same as dim[3,] and should therefore report TRUE if I am in position i=1 in the for loop. I could write another for loop to deal with this, but I am sure there are more clever and possibly vectorized ways to do this.

Upvotes: 2

Views: 46

Answers (1)

akrun
akrun

Reputation: 887028

We can use duplicated

duplicated(m1)|duplicated(m1, fromLast=TRUE)
#[1]  TRUE FALSE  TRUE FALSE

The duplicated(m1) gives a logical vector of 'TRUE/FALSE' values. If there is a duplicate row, it will be TRUE

 duplicated(m1)
 #[1] FALSE FALSE  TRUE FALSE

In this case, the third row is duplicate of first row. Suppose if we need both the first and third row, we can do the duplication from the reverse side and use | to make both positions TRUE. i.e.

 duplicated(m1, fromLast=TRUE)
 #[1]  TRUE FALSE FALSE FALSE

 duplicated(m1)|duplicated(m1, fromLast=TRUE)
 #[1]  TRUE FALSE  TRUE FALSE

According to ?duplicated, the input data can be

x: a vector or a data frame or an array or ‘NULL’.

data

 m1 <- cbind(x=c(5,2,5,5), y=c(1,2,1,9))

Upvotes: 4

Related Questions