Complement of empty index vector is empty index vector again

后端 未结 3 1986
梦谈多话
梦谈多话 2021-01-26 22:09

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

相关标签:
3条回答
  • 2021-01-26 22:49

    emptyindices is logical(0) (logical vector of length = 0) and -emptyindices becomes integer(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:

    v <- 6:10
    idx1 <- c(1,3)
    idx2 <- vector()
    idx3 <- 1:5
    
    v[setdiff(seq_along(v), idx1)]
    # [1] 7 9 10
    
    v[setdiff(seq_along(v), idx2)]
    # [1] 6 7 8 9 10
    
    v[setdiff(seq_along(v), idx3)]
    # integer(0)
    
    0 讨论(0)
  • 2021-01-26 22:55

    Thats because -0 = 0? But I can see how an algorithm can run into problem if this aspect is over-looked. So I suggest using setdiff instead of negative indices.

    0 讨论(0)
  • 2021-01-26 22:59

    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.

    0 讨论(0)
提交回复
热议问题