R: Err: 只有 0 可以与负下标混合 [英] R: Err: only 0's may be mixed with negative subscripts
问题描述
显然,我正在尝试使用正索引和负索引"对我的变量进行子集化.然而调试代码,我不明白为什么 R studio 会在我混合负子集和正子集时解释它.
Apparently I am trying to subset my variable with positive and "negative indexes". However debugging the code, I can not why R studio is interpreting it as I am mixing negative and positive subsets.
代码如下:
if (stepcount > 192 | sum(na.omit(ppt[i-193:i-1])) < 0.6) {statement}
错误:
Error in ppt[i - 193:i - 1] :
only 0's may be mixed with negative subscripts
调试代码我发现此时 i 的值为 3572,这意味着 ppt[i - 193:i - 1]
Debugging the code I see that my value for i at this point is 3572 which means nor negative subsetting in ppt[i - 193:i - 1]
如果有帮助,请提供更多信息:如果我使用两个或"运算符而不是一个,例如:
If it helps, just some more information: if I use two "or" operator instead of one, like:
if (stepcount > 192 || sum(na.omit(ppt[i-193:i-1])) < 0.6) {statement}
我仍然犯同样的错误,但我是 3603.老实说,我不知道区别,但可能它传达了某种可以澄清问题的信息.
I still get the same err but in i being 3603. Honestly I don't know the difference but may be it conveys some kind of information that could clarify the problem.
我认为这可能是一件我不知道的简单事情,并且不需要可重现的代码(因为它的代码太长而无法分析大数据.但是我可以将其发布,如果您伙计们认为这是必要的.
I think that this is probably a simple thing that I am not aware of and that the reproducible code would not be necessary (since it is a too long code to analyse a large data. However I could post it on, if you guys think that is necessary.
推荐答案
根据 Richard Scriven 的建议和 jlhoward 的解释,解决方案是将子集放在括号中:
As suggested by Richard Scriven and explained by jlhoward, the solution is to put the subset in parenthesis:
if (stepcount > 192 | sum(na.omit(ppt[(i-193):(i-1)])) < 0.6) {statement}
示例:
> k <- seq(from=1,to=1000,by=2)
> i <- 195
> k[(i - 193):(i - 1)]
[1] 3 5 7 9 11 13 15 17 19 21 23 25 27 29
[15] 31 33 35 37 39 41 43 45 47 49 51 53 55 ...
这篇关于R: Err: 只有 0 可以与负下标混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!