Skip to content
Prev 386642 / 398502 Next

Problem in cluster sampling: 'mixed with negative subscripts'

Hi Chao,
You have discovered one of the surprising things about the extraction
operator "[". It expects to get an object consisting of integers (like
1,2,3,...) or logical values (TRUE,FALSE or 0,1). As you have passed
the _values_ of your cluster, it can't deal with the negative numbers
as they don't index anything in the original object. If you get rid of
the negative values by applying the abs() function, it will seem to
work, but you aren't getting what you expected or anything sensible.
Here's an example:

# get a vector of positive and negative real numbers
x1<-rnorm(10)
x1
[1] -0.2174320 -1.3185389  0.4049751  0.4780766 -1.6317983  3.4265246
[7]  2.0721620  1.1590961  0.9896266  0.5672552
# try to index it with its values
x[x]
# Error in x[x] : only 0's may be mixed with negative subscripts
# now change the negative values to positive ones
 x1[abs(x1)]
# No error, but you only get some of the values!
[1] -0.2174320 -0.2174320  0.4049751 -1.3185389 -0.2174320
abs(x1)
[1] 0.2174320 1.3185389 0.4049751 0.4780766 1.6317983 3.4265246 2.0721620
[8] 1.1590961 0.9896266 0.5672552
# What the extraction operator does is attempt to get valid {positive}
integer indices
# or zeros. Then it can use the positive values and discard the zeros
as.integer(abs(x1))
[1] 0 1 0 0 1 3 2 1 0 0

Now the error message makes a lot more sense.

Jim
On Sun, Dec 20, 2020 at 5:58 AM Chao Liu <psychaoliu at gmail.com> wrote: