假定
x = c(1, 2, 3.5, 4, 6, 7.5, 8, 9, 10, 11.5, 12)
y = c(2.5, 6.5)
I = split(x, findInterval(x, y))
f = function(vec, x) {
d = pmax(outer(x, vec, "-"), 0)
colSums(d - d^2/2)
}
I want to calculate the value of f(I[[i]], x)
in each values of each interval and then find which I[[i]]
actual value has the maximum value of f(I[[i]], x )
in each interval.
Is there any other way which is more efficient than this for loop:
for (i in 1:length(I)) {
max.values[[i]] = I[[i]][which.max(f(I[[i]], x))]
}
这是我想要取得的结果:
> max.values
[1] 2 6 10