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

Chao Liu p@ych@o||u @end|ng |rom gm@||@com
Sun Dec 20 02:56:31 CET 2020


Jim and Bert,

Thank you for your help. It was an indexing problem. I was able to solve it
by changing the split line to: clusters <- split(seq_len(nrow(dd)),
dd$cluster).
Thank you.

Best,

Chao


On Sat, Dec 19, 2020 at 4:19 PM Jim Lemon <drjimlemon using gmail.com> wrote:

> 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list