Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negative subscripts error in R

Tags:

r

I have the following code snippet:

if(k<=100 && k>=0 )
{        
    j[k+seq(-50,150)]<-F;
}
else
{
    j[k+seq(-100,100)]<-F;
}

And the following error:

Error in j[k + seq(-50, 150)] <- F : only 0's may be mixed with negative subscripts

Why am I getting this even though I have set the conditions if the subscripts may run into the negative values?

like image 742
Concerned_Citizen Avatar asked Sep 05 '11 01:09

Concerned_Citizen


1 Answers

When k = 25, say, then your if condition is true (k is less than 100 but greater than 0). But 25 + (-50) is -25. But 25 + 150 = 175, a positive index. You can't mix positive and negative indices when subsetting.

I suppose I should add that part of the reason you can't do this is that positive and negative indices have different meaning. x[3] means you want to select the third element, whereas x[-3] means you want to omit the third element. It would get confusing to keep track of which indices referred to which elements if you started dropping elements at the same time you are selecting others.

like image 100
joran Avatar answered Sep 27 '22 18:09

joran