[R] question regarding vector ops
Rajarshi Guha
rajarshi at presidency.com
Fri Apr 9 18:33:44 CEST 2004
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?
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.
More information about the R-help
mailing list