Determining Minimum Values in a Vector in R

Determining minimum values in a vector in R

In order to find the index try this

which(x %in% sort(x)[1:3])  # this gives you and index vector
[1] 1 3 6

This says that the first, third and sixth elements are the first three lowest values in your vector, to see which values these are try:

x[ which(x %in% sort(x)[1:3])]  # this gives the vector of values
[1] 1 2 3

or just

x[c(1,3,6)]
[1] 1 2 3

If you have any duplicated value you may want to select unique values first and then sort them in order to find the index, just like this (Suggested by @Jeffrey Evans in his answer)

which(x %in% sort(unique(x))[1:3])

Return index of the smallest value in a vector?

You're looking for which.min():

a <- c(1,2,0,3,7,0,0,0)
which.min(a)
# [1] 3

which(a == min(a))
# [1] 3 6 7 8

(As you can see from the above, when several elements are tied for the minimum, which.min() only returns the index of the first one. You can use the second construct if you instead want the indices of all elements that match the minimum value.)

Creating a loop in R to find a minimum value of a vector by using comparison operators (, , ==, etc.)

This should do the trick. First create a variable called "low" with a high value (as @alistaire suggested). Then, loop through your values of aVector. At each value, check if the value is less than low. If it is, update low to that value.

set.seed(42)

aVector <- sample(0:100, 100, replace=FALSE)

low <- Inf # initialize with high value
for (i in aVector) {

if(i < low){
low <- i
}

}
print(low)

# Confirm we got correct answer
min(aVector)

In R, get the minimum value from vector of every group of n elements

here are some possibilities:

1) apply/matrix Form a matrix with 5 rows from distances stringing out the vector column by column and then take the minimum of each column:

apply(matrix(distances, 5), 2, min)
## [1] 2.30 0.75 4.40

2) zoo::rollapply An alternative is to use rollapply from the zoo package specifying that we wish to take the minimum of every 5 elements and skipping by 5 to the next set of 5 elements repeatedly.

library(zoo)

rollapply(distances, 5, by = 5, min)
## [1] 2.30 0.75 4.40

3) tapply/gl Since there are length(distances)/5 = 15/5 = 3 groups, each of length 5:

tapply(distances, gl(3, 5), min)
## 1 2 3
## 2.30 0.75 4.40

4) tapply/col This is similar to (3); however, in place of gl is uses col(matrix(...)) borwwing the matrix idea from (1):

tapply(distances, col(matrix(distances, 5)), min)
## 1 2 3
## 2.30 0.75 4.40

Finding min value for substracting each element of two vectors

We may have to consider the abs here in the function closest_longitude

closest_longitude <- function (x, y) {
which.min(abs(x - y))
}

sapply(a, closest_longitude, b)
#[1] 1 1 8 4 2 2 7 7 5

If we don't use the abs, it will always be 5

sapply(a, closest_longitude, b)
#[1] 5 5 5 5 5 5 5 5 5

because the 5th element in 'b' is 66 and it will give the lowest negative value while subtracting with each element of 'a' and resulting in selection of that.

Fastest way to find second (third...) highest/lowest value in vector or column

Rfast has a function called nth_element that does exactly what you ask.

Further the methods discussed above that are based on partial sort, don't support finding the k smallest values

Update (28/FEB/21) package kit offers a faster implementation (topn) see https://stackoverflow.com/a/66367996/4729755, https://stackoverflow.com/a/53146559/4729755

Disclaimer: An issue appears to occur when dealing with integers which can by bypassed by using as.numeric (e.g. Rfast::nth(as.numeric(1:10), 2)), and will be addressed in the next update of Rfast.

Rfast::nth(x, 5, descending = T)

Will return the 5th largest element of x, while

Rfast::nth(x, 5, descending = F)

Will return the 5th smallest element of x

Benchmarks below against most popular answers.

For 10 thousand numbers:

N = 10000
x = rnorm(N)

maxN <- function(x, N=2){
len <- length(x)
if(N>len){
warning('N greater than length(x). Setting N=length(x)')
N <- length(x)
}
sort(x,partial=len-N+1)[len-N+1]
}

microbenchmark::microbenchmark(
Rfast = Rfast::nth(x,5,descending = T),
maxn = maxN(x,5),
order = x[order(x, decreasing = T)[5]])

Unit: microseconds
expr min lq mean median uq max neval
Rfast 160.364 179.607 202.8024 194.575 210.1830 351.517 100
maxN 396.419 423.360 559.2707 446.452 487.0775 4949.452 100
order 1288.466 1343.417 1746.7627 1433.221 1500.7865 13768.148 100

For 1 million numbers:

N = 1e6
x = rnorm(N)

microbenchmark::microbenchmark(
Rfast = Rfast::nth(x,5,descending = T),
maxN = maxN(x,5),
order = x[order(x, decreasing = T)[5]])

Unit: milliseconds
expr min lq mean median uq max neval
Rfast 89.7722 93.63674 114.9893 104.6325 120.5767 204.8839 100
maxN 150.2822 207.03922 235.3037 241.7604 259.7476 336.7051 100
order 930.8924 968.54785 1005.5487 991.7995 1031.0290 1164.9129 100

Minimum and maximum sequential values of a vector

You can do:

range(v[which.min(v):length(v)])

[1] 1 7

Find min value between 2 sets of vectors (fastest way)

I think the easiest and possibly fastest way to do it is as follows:

apply(dat, 1, function(d) {
overlap <- (dat$y.end >= d[1] & dat$y.end <= d[2]) |
(dat$y.start >= d[1] & dat$y.start <= d[2])
if (any(overlap)) 0
else min(abs(c(d[1] - dat$y.end[!overlap], dat$y.start[!overlap] - d[2])))
})

EDIT: overlap can be much simpler:

apply(dat, 1, function(d) {
overlap <- dat$y.end >= d[1] & dat$y.start <= d[2]
if (any(overlap)) 0
else min(abs(c(d[1] - dat$y.end[!overlap], dat$y.start[!overlap] - d[2])))
})


Related Topics



Leave a reply



Submit