문제

I'm stuck with an easy task, I really hope someone could help me with this..

I have a vector with values close to each other and distinct gaps in between. What I need is this vector minimized to a vector with every first value of all high density series.

Example:

original_vector <- c(0.001,0.002,0.003,0.004,0.1001,0.1002,0.1003,0.1004,0.1005,
                     0.5003,0.5004)
wanted_vector <- c(0.001,0.1001,0.5003)

Does someone know how to convert the original vector into the wanted vector?

Thanks in advance!

도움이 되었습니까?

해결책

You'll of course first need to define what you consider low vs. high density.

Assuming you can decide on a cutoff distance between consecutive elements, tol, below which two consecutive elements are considered to be densely packed, you could use something like this:

f <- function(x, tol) {
    x[tol < diff(c(-Inf, x))]
}


original_vector <- c(0.001,0.002,0.003,0.004,0.1001,0.1002,0.1003,0.1004,0.1005,
                     0.5003,0.5004)

f(original_vector, tol = 0.002)
# [1] 0.0010 0.1001 0.5003
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top