Chao Liu
2020-Dec-19 18:57 UTC
[R] Problem in cluster sampling: 'mixed with negative subscripts'
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]]
Bert Gunter
2020-Dec-19 20:03 UTC
[R] Problem in cluster sampling: 'mixed with negative subscripts'
1. Thanks for the example. 2. Good opportunity to learn (more) about debugging in R. See ?debug or ?browser() 3. Hint: what do you think ... unlist(clusters[... gives? (you are using it as an index for subscripting dd) Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sat, Dec 19, 2020 at 10:58 AM Chao Liu <psychaoliu at 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 at 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]]
Jim Lemon
2020-Dec-19 21:19 UTC
[R] 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:> > 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 at 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.