I know this question was already posted but the answer was a trick to solve the given problem some other way, but the core question remained unanswered.
The question is this.
somevector <- 1:5
emptyindeces <- vector()
somevector[-emptyindeces] #returns empty vector
Why it is not the original vector?
Is there a reason for that or am I understanding it wrong. If so whats the correct way to get the complement of an index vector.
somevector <- c(1:5,NA,8)
sumvec<-subset(somevector,!is.na(somevector))
Not too sure if this is what you want, but let me know if you wanted something different so I can correct my answer. The answer hashtagged is an alternative answer if this is what you were looking for.
emptyindices
islogical(0)
(logical vector of length = 0) and-emptyindices
becomesinteger(0)
. So, you're querying the vector with indices of length = 0. You get back a length = 0 integer vector.Probably you are looking for, for example,
setdiff
:Thats because
-0 = 0
? But I can see how an algorithm can run into problem if this aspect is over-looked. So I suggest usingsetdiff
instead of negative indices.