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
Problem in cluster sampling: 'mixed with negative subscripts'
9 messages · Chao Liu, Richard O'Keefe, Bert Gunter +3 more
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.
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.
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 at 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 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.
More accurately, in x[i] where x and i are simple vectors, i may be a mix of positive integers and zeros where the zeros contribute nothing to the result or it may be a MIX of negative integers and zeros where the zeros contribute nothing to the result and -k means "do not include element k". It would be nice to write things like x[c(-1,1)] meaning to copy everything except element 1, then element 1. But that is not allowed.
On Sun, 20 Dec 2020 at 10:20, Jim Lemon <drjimlemon at 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 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. ______________________________________________ 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.
Inline...
On Sat, Dec 19, 2020 at 9:22 PM Richard O'Keefe <raoknz at gmail.com> wrote:
More accurately, in x[i] where x and i are simple vectors, i may be a mix of positive integers and zeros where the zeros contribute nothing to the result or it may be a MIX of negative integers and zeros where the zeros contribute nothing to the result and -k means "do not include element k". It would be nice to write things like x[c(-1,1)] meaning to copy everything except element 1, then element 1. But that is not allowed.
But c(x[-1], x[1]) is, which is not so terrible, after all... Cheers, Bert
On Sun, 20 Dec 2020 at 10:20, Jim Lemon <drjimlemon at 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 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. ______________________________________________ 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]]
______________________________________________ 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.
I've never mastered using negative indexes with my fingers, though...
On December 19, 2020 11:32:46 PM PST, Jim Lemon <drjimlemon at gmail.com> wrote:
It does remind me of counting on one's fingers, though. Jim On Sun, Dec 20, 2020 at 4:38 PM Bert Gunter <bgunter.4567 at gmail.com> wrote:
But c(x[-1], x[1]) is, which is not so terrible, after all...
______________________________________________ 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.
Sent from my phone. Please excuse my brevity.
On Sun, 20 Dec 2020 at 06:22, Richard O'Keefe <raoknz at gmail.com> wrote:
More accurately, in x[i] where x and i are simple vectors, i may be a mix of positive integers and zeros where the zeros contribute nothing to the result
Yes, index 0 doesn't get an error or a warning. I think it should. Eg. (v <- 1:4) [1] 1 2 3 4 v[c(0,2,4)] <- c(0,2) v [1] 1 0 3 2 # a surprise ? Regards Martin