Search code examples
rnumeric

Find the nearest bigger number in R


I have a dataset like this:

row  num Group
  1 3     B
  2 6     A
  3 12    A
  4 15    B
  5 16    A
  6 18    A
  7 20    B
  8 25    A
  9 27    B
 10 29    B

In R, I would like to compare an input number with the values in num, and I would like to find the location of the closest bigger value in Group A only.

For example, if the input number is 8, then the closest, bigger value in group A should be 12, and I would like to get its location which should be 3. If the input is 18, then the value returned should be 18, and the location should be 6. If the input is 20, then the value returned should be 25, and the location should be 8.

I tried which.min, but for some reason, index 1 is always returned regardless of my imput number.

#called the dataset f

which.min(f$num[f$Group=="A"][f$num[f$Group=="A"]>=8])

I would like to still use base R if possible I would appreciate any thoughts on which part I did wrong and how to fix it.

Thank you.


Solution

  • Use ifelse() to replace elements that don’t meet your conditions with NA, then use which.min() on the resulting vector:

    which.min(ifelse(f$Group == "A" & f$num >= 8, f$num, NA))
    # 3
    
    which.min(ifelse(f$Group == "A" & f$num >= 18, f$num, NA))
    # 6
    

    Part of the reason your solution doesn’t work is that by subsetting, you change the element positions, so the position returned by which.min() doesn’t correspond to your original vector. By replacing with NAs instead, you preserve the original positions.