[General] compare matrix to vector higher/lower
1
0
Entering edit mode
BenAbflack • 0
@benabflack-8435
Last seen 8.8 years ago
Zurich/Switzerland

Hey everyone,

I have a bit more of a general R question which I cant really figure out. I have a matrix of the type

   [A] [B] [C]
[1] 3   5   2
[2] 1   3   6
[3] 1   6   2

and a vector in the form

   [A] [B] [C]
[1] 2   4   7

What I am trying to get out is in this case 3 new columns attached to the initial matrix which would lets say go through all rows in column A and compare it to the value for [A] in the vector. If its higher than I would like to have a 1 in the new column, if lower a 0. And then for B and so on. In this example:

   [A] [B] [C] [A.2][B.2][C.2]
[1] 3   5   2    1    1    0
[2] 1   3   6    0    0    0
[3] 1   6   2    0    1    0

Anyone knows a good way to do this? I think I am getting too confused with multiple loops.

Thank you so much,

Ben

matrix • 919 views
0
Entering edit mode
@andrewjskelton73-7074
Last seen 4 months ago
United Kingdom

This isn't the place to ask that question, you should probably try stack overflow for this sort of thing. However, it's early and I fancied a go...

foo           <- matrix(sample(1:20, 9),3,3)
baa           <- c(8,5,12)
out           <- baa < t(foo)
out[out == F] <- 0
out[out == T] <- 1