Reputation: 77
I have a 4 x 600 datatable with 3 numeric columns with random integers ranging from 0 - 700. I have defined a variable as an integer with a value of 542. I am attempting to identify the minimum value that is greater than 542 in each of the numeric columns. However, the following works but is pulling the wrong value, any idea why? Or if I am doing something wrong?
#data.table called df with numeric columns being b,c,d
a <- as.integer(542)
min(which(df$b > a))
[1] 131
How is that possible? I know the correct value should be 543 for this column.
Upvotes: 1
Views: 56
Reputation: 101628
You should know that which.min
is equivalent to
which(x == min(x, na.rm = TRUE))
and it is definitely different from min(which(x))
Upvotes: 1
Reputation: 887153
The output from which
is a position index and the the min
applied on which
returns the minimum position and not the position of minimum value. Use the output from which
to extract the values in 'b' and then wrap with min
min(df$b[which(df$b > a)])
Upvotes: 1