Let's assume we have a data.frame
like the following:
set.seed(2019)
datos <- data.frame(ANO1=sample(1:10, 10, replace = TRUE),
ANO2=sample(1:10, 10, replace = TRUE))
datos
ANO1 ANO2
1 8 8
2 8 7
3 4 3
4 7 2
5 1 7
6 1 7
7 9 1
8 1 8
9 2 4
10 7 5
What I am looking for is to create a matrix with the logical value of comparing if the two columns are less than a certain set of numbers, for example, let's say a range from 1 to 10, taking the first row as a reference, I would like to obtain something like this :
ANO1 ANO2 1 2 3 4 5 6 7 8 9 10
1 8 8 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE
In this case 8
they are both less than 10
and 9
but not less than the rest of the values.
One way that occurs to me is to create a reference matrix to compare, that is, we are going to create a matrix with the 10 "comparables" and the number of rows of the
data.frame
, that is:Having this, and thanks to the fact that the comparisons are actually functions, we could individually compare each column with its reference value:
Note: I originally thought it wouldn't work, but it's also totally fine to do:
And now we simply make sure that both columns are less than the reference value with a
&
(and
logical matrix), the combination of the results is only for the purpose of verifying the result: