空索引向量的补充再次是空索引向量 [英] Complement of empty index vector is empty index vector again

查看:158
本文介绍了空索引向量的补充再次是空索引向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经发布,但答案是以其他方式解决给定问题的一个技巧,但核心问题仍然没有答案。

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.

问题是这个。

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.

推荐答案

emptyindices logical(0)(长度为0的逻辑向量)和 -emptyindices 变为整数(0)。所以,你要查询长度为0的索引向量。你得到一个长度= 0的整数向量。

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.

可能你正在寻找,例如, setdiff

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)

这篇关于空索引向量的补充再次是空索引向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆