Given the following:
a <- c(1,2,3)
b <- c(1,2,3)
c <- c(4,5,6)
A <- cbind(a,b,c)
I want to find which columns in A are equal to for example my vector a.
My first attempt would be:
> which(a==A)
[1] 1 2 3 4 5 6
Which did not do that. (Too be honest I don't even understand what that did) Second attempt was:
a==A
a b c
[1,] TRUE TRUE FALSE
[2,] TRUE TRUE FALSE
[3,] TRUE TRUE FALSE
which definitely is a step in the right direction but it seems extended into a matrix. What I would have preferred is something like just one of the rows. How do I compare a vector to columns and how do I find columns in a matrix that are equal to a vector?
If you add an extra row:
> A
a b c
[1,] 1 1 4 4
[2,] 2 2 5 2
[3,] 3 3 6 1
Then you can see that this function is correct:
> hasCol=function(A,a){colSums(a==A)==nrow(A)}
> A[,hasCol(A,a)]
a b
[1,] 1 1
[2,] 2 2
[3,] 3 3
But the earlier version accepted doesn't:
> oopsCol=function(A,a){colSums(a==A)>0}
> A[,oopsCol(A,a)]
a b
[1,] 1 1 4
[2,] 2 2 2
[3,] 3 3 1
It returns the 4,2,1 column because the 2 matches the 2 in 1,2,3.