I am trying to find a way to find the minimums of a column in a matrix dependent on values in another column. Suppose my matrix looks like so:
A B
3 1.2
3 3.4
3 0.7
4 4.5
4 4.9
5 0.1
5 0.4
6 5
I wish to find the "local maximum" of B for each column sharing the same numbers in A.
So, I would like to get a new variable C, that looks like:
A B C
3 1.2 0.7
3 3.4 0.7
3 0.7 0.7
4 4.5 4.5
4 4.9 4.5
5 0.1 0.1
5 0.4 0.1
6 5 5
As one can see, the values in C is the minimum value of entries of B sharing the same value in A.
for(i in 1:length(data)){
if (A[i]==A[i+1])
else C <- min(B[i])
}
A for loop above like this might do the trick, but gets very computationally intensive and expensive. I was wondering if there was a simpler way to do this in creating a column with minimum values. Thanks!