As the title says. I have a vector and I want to sum all elements in the vector, except the ith element. I can achieve this with a for-loop e.g.
# vector to iterate over
v1 <- 1:5
# somewhere to store results
v2 <- c()
for (i in seq_along(v1)) {
v2[i] <- sum(v1[-i])
}
# desired output
v2
[1] 14 13 12 11 10
I am looking for performance gains, Is it possible to achieve v2 without a for-loop via vectorisation?