Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all rows of matrix equal to vector

Tags:

r

Suppose I have the following matrix:

cm<-structure(c(100, 200, 400, 800, 100, 200, 400, 800, 100, 200, 
400, 800, 100, 200, 400, 800, 100, 200, 400, 800, 0, 0, 0, 0, 
0.5, 0.5, 0.5, 0.5, 1, 1, 1, 1, 0, 0, 0, 0, 0.5, 0.5, 0.5, 0.5, 
-0.4, -0.4, -0.4, -0.4, -0.4, -0.4, -0.4, -0.4, -0.4, -0.4, -0.4, 
-0.4, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1), .Dim = c(20L, 4L), .Dimnames = list(
    NULL, c("Var1", "Var2", "Var3", "n1")))

and another matrix derived from it:

a4<-data.matrix(unique(cm[,1:3]))

Now, I want to find all the rows of cm whose first three columns are equal to a4[1,], but doing the intutive thing:

a5<-which(cm[,1:3]==a4[1,])

fails (R 3.1.3). For example a5[2] is 13, but the 13th row of cm[,1:3] ain't the same as a4[1,].

like image 321
user189035 Avatar asked Mar 30 '15 11:03

user189035


3 Answers

Use apply and all.equal to compare each row against the target row. The problem with using == is that it only checks the it recycles elements of a vector for comparison, whereas you want to see if all values in the row vector match a4[1,] so you should use all.equal. The consequence is that it's return value is not a logical but instead a character string describing differences between the objects, which makes it a little messier to work with than == alone:

which(apply(cm, 1, function(x) all.equal(x[1:3], a4[1,])) == "TRUE")
# [1] 1

You can also make that a bit simpler by using identical instead of all.equal:

which(apply(cm, 1, function(x) identical(x[1:3], a4[1,])))
# [1] 1

Then extract:

cm[apply(cm, 1, function(x) identical(x[1:3], a4[1,])),,drop=FALSE]
#      Var1 Var2 Var3 n1
# [1,]  100    0 -0.4  1

To clarify exactly what's happening, consider what == does implicitly when you pass a matrix argument:

which(cm[,1:3]==a4[1,])
# [1]  1 13 23 35 42 45 48 51 53 56 59

That result is the same as converting the matrix to a vector:

as.vector(cm[,1:3])
#  [1] 100.0 200.0 400.0 800.0 100.0 200.0 400.0 800.0 100.0 200.0 400.0 800.0 100.0 200.0 400.0 800.0 100.0 200.0 400.0 800.0   0.0   0.0   0.0   0.0   0.5   0.5   0.5
# [28]   0.5   1.0   1.0   1.0   1.0   0.0   0.0   0.0   0.0   0.5   0.5   0.5   0.5  -0.4  -0.4  -0.4  -0.4  -0.4  -0.4  -0.4  -0.4  -0.4  -0.4  -0.4  -0.4   0.0   0.0
# [55]   0.0   0.0   0.0   0.0   0.0   0.0
which(as.vector(cm[,1:3])==a4[1,])
# [1]  1 13 23 35 42 45 48 51 53 56 59

Thus, the positions are positions within the vector representation of cm, not rows in the matrix representation. == comparisons can also be dangerous (again do to the recycling noted above) when trying to compare vectors that are not of equivalent length or where one vector's length is not a multiple of the other, which will produce a warning:

1:2 == 1:3
# [1]  TRUE  TRUE FALSE
# Warning message:
# In 1:2 == 1:3 :
#   longer object length is not a multiple of shorter object length

Whereas there is no warning when recycling is used:

1:2 == 1:6
# [1]  TRUE  TRUE FALSE FALSE FALSE FALSE
like image 160
Thomas Avatar answered Nov 19 '22 15:11

Thomas


The function row.match in prodlim is easy to use, and ideal for your problem.

library(prodlim)
row.match(a4[1,], cm[,1:3])
[1] 1
like image 8
milan Avatar answered Nov 19 '22 16:11

milan


Storing the row to be matched and main table as dataframes and calling plyr's match_df.

Firstly, understanding the matching process.

plyr::match_df(data.frame(cm[,1:3]),
           data.frame(t(a4[1,])))

# Matching on: Var1, Var2, Var3
# Var1 Var2 Var3
# 1  100    0 -0.4

Now, using another case. Matching using two columns and extracting the rownumber to slice the dataframe.

cm[plyr::match_df(data.frame(cm[,c(1,3)]),
               data.frame(t(cm[3,c(1,3)]))) %>% rownames() %>% as.numeric(),]

# 
# Matching on: Var1, Var3
# Var1 Var2 Var3 n1
# [1,]  400  0.0 -0.4  1
# [2,]  400  0.5 -0.4  1
# [3,]  400  1.0 -0.4  1
like image 1
Priyesh Mehta Avatar answered Nov 19 '22 14:11

Priyesh Mehta