简体   繁体   English

为什么在 R 中减去一个空向量会删除所有内容?

[英]Why subtracting an empty vector in R deletes everything?

Could someone please enlighten me why subtracting an empty vector in R results in the whole content of a data frame being deleted?有人可以告诉我为什么在 R 中减去一个空向量会导致数据框的全部内容被删除? Just to give an example举个例子

WhichInstances2 <- which(JointProcedures3$JointID %in% KneeIDcount$Var1[KneeIDcount$Freq >1])

JointProcedures3 <-JointProcedures3[-WhichInstances2,] 

Will give me all blanks in JointProcedures3 if WhichInstances2 has all its value as FALSE, but it should simply give me what JointProcedures3 was before those lines of code.如果 WhichInstances2 的所有值都为 FALSE,它将给我 JointProcedures3 中的所有空白,但它应该简单地告诉我 JointProcedures3 在这些代码行之前是什么。

This is not the first time it has happened to me and I have asked my supervisor and it has happened to him as well and he just thinks t is a quirk of R.这不是第一次发生在我身上,我问过我的主管,他也遇到过,他只是认为这是 R 的怪癖。

Rewriting the code as将代码重写为

WhichInstances2 <- which(JointProcedures3$JointID %in% KneeIDcount$Var1[KneeIDcount$Freq >1])

if(length(WhichInstances2)>0)
{
  JointProcedures3 <-JointProcedures3[-WhichInstances2,]
}

fixes the issue.解决了这个问题。 But it should not have in principle made a scooby of a difference if that conditional was there or not, since if length(WhichInstances2) was equal to 0, I would simply be subtract nothing from the original JointProcedures3...但是,如果该条件是否存在,原则上它不应该造成差异,因为如果长度(WhichInstances2)等于0,我只会从原始的JointProcedures3中减去任何内容......

Thanks all for your input.感谢大家的意见。

It seems you are checking for ids in a vector and you intend to remove them from another;看来您正在检查向量中的 id,并且打算从另一个向量中删除它们; probably setdiff is what you are looking for.可能setdiff是您正在寻找的。

Consider if we have a vector of the lowercase letters of the alphabet (its an r builtin) and we want to remove any entry that matches something that is not in there ("ab"), as programmers we would wish for nothing to be removed and keep our 26 letters考虑如果我们有一个小写字母的向量(它是一个 r 内置)并且我们想要删除任何与不存在的内容(“ab”)匹配的条目,作为程序员,我们希望没有任何内容被删除并保留我们的 26 个字母

# wont work
letters[ - which(letters=="ab")]

#works
setdiff(letters  , which(letters=="ab"))
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u"
[22] "v" "w" "x" "y" "z"

Let's try a simpler example to see what's happening.让我们尝试一个更简单的例子来看看发生了什么。

x <- 1:5
y <- LETTERS[1:5]
which(x>4)
## [1] 5
y[which(x>4)]
## [1] "E"

So far so good...到目前为止,一切都很好...

which(x>5)
## integer(0)
> y[which(x>5)]
## character(0)

This is also fine.这也很好。 Now what if we negate?现在如果我们否定呢? The problem is that integer(0) is a zero-length vector, so -integer(0) is also a zero-length vector, so y[-which(x>5] is also a zero-length vector..问题是integer(0)是一个零长度向量,所以-integer(0)也是一个零长度向量,所以y[-which(x>5]也是一个零长度向量..

What can you do about it?你能为这个做什么? Don't use which() ;不要使用which() instead use logical indexing directly, and use !而是直接使用逻辑索引,并使用! to negate the condition:否定条件:

y[!(x>5)]
## [1] "A" "B" "C" "D" "E"

In your case:在你的情况下:

JointID_OK <- (JointProcedures3$JointID %in% KneeIDcount$Var1[KneeIDcount$Freq >1])

JointProcedures3 <-JointProcedures3[!JointID_OK,] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM