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