Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare matrix with elements in vector by row

Tags:

r

matrix

I would like to compare values of a matrix to a corresponding vector. The comparison should be performed row-wise.

Example matrix:

ret

                    TLT          VTI
1995-01-20 -0.005649718 -0.004461441
1995-01-23 -0.002840909  0.002560820
1995-01-24  0.000000000  0.000000000
1995-01-25  0.005698006  0.003831418
1995-01-26  0.000000000  0.001908397

VEctor to compare with:

compare.vec
[1] -0.001  -0.002

What I want is to compare each return element in column 1 of ret to -0.001 and find which one is smaller than -0.001. Vice versa for the second column, comparing it to -0.002 and finding the elements in VTI volumn that are less than that.

I tried subset, but it seems its not for a vector but a number. Will I need to loop column for column?

Thanks,

like image 939
user1234440 Avatar asked May 09 '13 18:05

user1234440


1 Answers

All you have to do is:

t(t(ret) < compare.vec)

EDIT based on comment from Arun.

like image 200
Thomas Avatar answered Oct 30 '22 10:10

Thomas