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

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Sat Dec 19 21:03:49 CET 2020


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