0

For example suppose I have matrix A

  x  y z    f
1 1  2 A 1005
2 2  4 B 1002
3 3  2 B 1001
4 4  8 C 1001
5 5 10 D 1004
6 6 12 D 1004
7 7 11 E 1005
8 8 14 E 1003

From this matrix I want to find the repeated values like 1001, 1005, D, 2 (in third column) and I also want to find their index (which row, or which position).

I am new to R! Obviously it is possible to do with simple searching element by element by using a for loop, but I want to know, is there any function available in R for this kind of problem.

Furthermore, I tried using duplicated and unique, both functions are giving me the duplicated row number or column number, they are also giving me how many of them were repeated, but I can not search for whole matrix using both of them!

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • 1
    Show the code you actually tried so we can help you with it. Also, it seems like you have a data.frame rather than a matrix, no? Check with `class(A)`. – MrFlick Feb 23 '16 at 23:36
  • Thanks for your reply. first I tried with data frame, could not sort it out, then converted a into matrix using as.matrix Code I tried >duplicated(A) >FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE >x[duplicated(x)] >data frame with 0 columns and 8 rows But if I use single column or matrix it can find that, but not fore a whole data set or matrix Then I tried this unique(A[duplicated(A),]) – Al Mamun Feb 23 '16 at 23:43
  • 2
    You can't have mixed data types in a matrix so did you convert all those values to characters? That's not clear. You should make your example more [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and be explicit about the desired output for your sample data. What exactly do you want returned here? What type of object? – MrFlick Feb 23 '16 at 23:45

1 Answers1

0

You can write a rather simple function to get this information. Though note that this solution works with a matrix. It does not work with a data.frame. A similar function could be written for a data.frame using the fact that the data.frame data structure is a subset of a list.

# example data
set.seed(234)
m <- matrix(sample(1:10, size=100, replace=T), 10)

find_matches <- function(mat, value) {
  nr <- nrow(mat)
  val_match <- which(mat == value)
  out <- matrix(NA, nrow= length(val_match), ncol= 2)
  out[,2] <- floor(val_match / nr) + 1
  out[,1] <- val_match %% nr
  return(out)
}

R> m 
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    8    6    6    7    6    7    4   10    6     9
 [2,]    8    6    6    3   10    4    5    4    6     9
 [3,]    1    6    9    2    9    2    3    6    4     2
 [4,]    8    6    7    8    3    9    9    4    9     2
 [5,]    1    1    5    6    7    1    5    1   10     6
 [6,]    7    5    4    7    8    2    4    4    7    10
 [7,]   10    4    7    8    3    1    8    6    3     4
 [8,]    8    8    2    2    7    5    6    4   10     4
 [9,]   10    2    9    6    6    9    7    2    4     7
[10,]    3    9    9    4    2    7    7    2    9     6
R> find_matches(m, 8)
      [,1] [,2]
 [1,]    1    1
 [2,]    2    1
 [3,]    4    1
 [4,]    8    1
 [5,]    8    2
 [6,]    4    4
 [7,]    7    4
 [8,]    6    5
 [9,]    7    7

In this function, the row index is output in column 1 and the column index is output in column 2

alexwhitworth
  • 4,839
  • 5
  • 32
  • 59