Samples of equidistant points from a numerical vector

I have a numerical vector:

vec = c(1464.556644,552.6007169,155.4249747,1855.360016,1315.874155,2047.980206,2361.475519,4130.530507,1609.572131,4298.980363,697.6034771,312.080866,2790.738644,1116.406288,989.6391649,2683.393338,3032.080837,2462.137352,2964.362507,1182.894473,1268.968128,4495.503015,576.1063996,232.4996213,1355.256694,1336.607876,2506.458008,1242.918255,3645.587384) 

and I would like to select n=5 points from it that are as equidistant from each other as possible. In other words, I would like to get the points from vec that are closest to these points:

 seq(min(vec),max(vec),(max(vec)-min(vec))/(n-1)) 

What is the fastest way to achieve this?

+6
source share
2 answers

I supported thelatemail, this would also be my quick and dirty solution if it hadn’t published so fast :-)

I think a more robust approach is to solve integer programming. This, for example, will prevent the ability to select the same point more than once.

 n <- 5 N <- length(vec) ideal <- seq(min(vec),max(vec),(max(vec)-min(vec))/(n-1)) library(lpSolve) cost.mat <- outer(ideal, vec, function(x, y) abs(xy)) row.signs <- rep("==", n) row.rhs <- rep(1, n) col.signs <- rep("<=", N) col.rhs <- rep(1, N) sol <- lp.transport(cost.mat, "min", row.signs, row.rhs, col.signs, col.rhs)$solution final <- vec[apply(sol, 1, which.max)] 

This will certainly be slower, but this is the only "optimal and 100% reliable" way, in my opinion.

+6
source

Attempt:

 ideal <- seq(min(vec),max(vec),(max(vec)-min(vec))/(n-1)) result <- sapply(ideal, function(x) vec[which.min(abs(vec-x))] ) 

For comparison:

 cbind(result,ideal) result ideal [1,] 155.425 155.425 [2,] 1242.918 1240.444 [3,] 2361.476 2325.464 [4,] 3645.587 3410.484 [5,] 4495.503 4495.503 
+3
source

All Articles