Hi, I was playing with some code and came upon this situation.> x <- c(1, rep(0,9)) > x[1] 1 0 0 0 0 0 0 0 0 0> idx <- 1:10 > x[idx] <- x[idx] + 1 > x[1] 2 1 1 1 1 1 1 1 1 1 This is expected. But if I do:> x <- c(1, rep(0,9)) > x[1] 1 0 0 0 0 0 0 0 0 0> idx <- rep(0,10) > idx[1] 0 0 0 0 0 0 0 0 0 0> x[idx] <- x[idx] +1 > x[1] 1 0 0 0 0 0 0 0 0 0 I was expoecting that when all the elements of idx are set to 0, x[0] would become 11. Could somebody explain why this behavior occurs? Thanks, ------------------------------------------------------------------- Rajarshi Guha <rxg218 at psu.edu> <http://jijo.cjb.net> GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE ------------------------------------------------------------------- A computer lets you make more mistakes faster than any other invention, with the possible exceptions of handguns and Tequilla. -- Mitch Ratcliffe
Rajarshi Guha wrote:> Hi, > I was playing with some code and came upon this situation. > > >>x <- c(1, rep(0,9)) >>x > > [1] 1 0 0 0 0 0 0 0 0 0 > >>idx <- 1:10 >>x[idx] <- x[idx] + 1 >>x > > [1] 2 1 1 1 1 1 1 1 1 1 > > This is expected. But if I do: > > >>x <- c(1, rep(0,9)) >>x > > [1] 1 0 0 0 0 0 0 0 0 0 > >>idx <- rep(0,10) >>idx > > [1] 0 0 0 0 0 0 0 0 0 0 > >>x[idx] <- x[idx] +1 >>x > > [1] 1 0 0 0 0 0 0 0 0 0 > > I was expoecting that when all the elements of idx are set to 0, > x[0] would become 11. > > Could somebody explain why this behavior occurs?Why do you expect anything to become 11? And what is x[0] in your expectation? x[0] is "empty" (indexing is starting from 1 in R) and returns numeric(0), hence x[0] <- anything consequently does nothing ... (alternatively, one might expect an error message). Uwe Ligges> Thanks, > > > ------------------------------------------------------------------- > Rajarshi Guha <rxg218 at psu.edu> <http://jijo.cjb.net> > GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE > ------------------------------------------------------------------- > A computer lets you make more mistakes faster than any other invention, > with the possible exceptions of handguns and Tequilla. > -- Mitch Ratcliffe > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
On Fri, 2004-04-09 at 12:03, Rajarshi Guha wrote:> Hi, > I was playing with some code and came upon this situation. > > > x <- c(1, rep(0,9)) > > x > [1] 1 0 0 0 0 0 0 0 0 0 > > idx <- 1:10 > > x[idx] <- x[idx] + 1 > > x > [1] 2 1 1 1 1 1 1 1 1 1 > > This is expected. But if I do: > > > x <- c(1, rep(0,9)) > > x > [1] 1 0 0 0 0 0 0 0 0 0 > > idx <- rep(0,10) > > idx > [1] 0 0 0 0 0 0 0 0 0 0 > > x[idx] <- x[idx] +1 > > x > [1] 1 0 0 0 0 0 0 0 0 0Aah, I realize that the second code snippet is not supposed to work. What I meant was:> x <- c(1, rep(0,9)) > x[1] 1 0 0 0 0 0 0 0 0 0> idx <- rep(1,10) >idx[1] 1 1 1 1 1 1 1 1 1 1> x[idx] <- x[idx] + 1[1] 2 0 0 0 0 0 0 0 0 0 I expected that x would be [1] 11 0 0 0 0 0 0 0 0 0 Or am I thinking about it wrong? Thanks, ------------------------------------------------------------------- Rajarshi Guha <rxg218 at psu.edu> <http://jijo.cjb.net> GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE ------------------------------------------------------------------- Disembowelling takes guts.
Rajarshi Guha wrote:> On Fri, 2004-04-09 at 12:03, Rajarshi Guha wrote: > >>Hi, >> I was playing with some code and came upon this situation. >> >> >>>x <- c(1, rep(0,9)) >>>x >> >> [1] 1 0 0 0 0 0 0 0 0 0 >> >>>idx <- 1:10 >>>x[idx] <- x[idx] + 1 >>>x >> >> [1] 2 1 1 1 1 1 1 1 1 1 >> >>This is expected. But if I do: >> >> >>>x <- c(1, rep(0,9)) >>>x >> >> [1] 1 0 0 0 0 0 0 0 0 0 >> >>>idx <- rep(0,10) >>>idx >> >> [1] 0 0 0 0 0 0 0 0 0 0 >> >>>x[idx] <- x[idx] +1 >>>x >> >> [1] 1 0 0 0 0 0 0 0 0 0 > > > Aah, I realize that the second code snippet is not supposed to work. > What I meant was: > > >>x <- c(1, rep(0,9)) >>x > > [1] 1 0 0 0 0 0 0 0 0 0 > >>idx <- rep(1,10) >>idx > > [1] 1 1 1 1 1 1 1 1 1 1 > >>x[idx] <- x[idx] + 1 > > [1] 2 0 0 0 0 0 0 0 0 0 > > I expected that x would be > > [1] 11 0 0 0 0 0 0 0 0 0 > > Or am I thinking about it wrong?Yes, you are wrong: You are indexing 10 times the first value. Writing 10 times the same value 10 times into the same location does not sum it up. Consider x[idx] <- 1:10 Then you are writing 1 into x[1], 2 into x[1], 3 into x[1], ... and finally 10 into x[1], hence at the end x[1] is 10. Uwe Ligges> Thanks, > > ------------------------------------------------------------------- > Rajarshi Guha <rxg218 at psu.edu> <http://jijo.cjb.net> > GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE > ------------------------------------------------------------------- > Disembowelling takes guts. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html