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

Chao Liu p@ych@o||u @end|ng |rom gm@||@com
Sat Dec 19 19:57:29 CET 2020


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



More information about the R-help mailing list