[R] Problem in cluster sampling: 'mixed with negative subscripts'

Jim Lemon drj|m|emon @end|ng |rom gm@||@com
Sat Dec 19 22:19:28 CET 2020


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 using gmail.com> wrote:
>
> Hi,
>
> I was trying to do a cluster sampling but came across this error: Error in
> xj[i] : only 0's may be mixed with negative subscripts. What is the cause
> and how to get around? Thank you for your help!
>
> Here is the code:
>
> #simulate some data
> y <- rnorm(20)
> x <- rnorm(20)
> z <- rep(1:5, 4)
> w <- rep(1:4, each=5)
> dd <- data.frame(id=z, cluster=w, x=x, y=y)
> clusters <- split(dd, dd$cluster) #split into clusters
> k <- length(clusters) #length of clusters
> # This function generates a cluster sample
> clsamp <- function() dd[unlist(clusters[sample.int(k, k, replace = TRUE)],
> use.names = TRUE), ]
> clsamp()
>
>
> I got this error: Error in xj[i] : only 0's may be mixed with negative
> subscripts.
>
>
> Best,
>
> Chao
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list