Re: [R] flip certain bits in vector

2009-04-19 Thread Esmail
jim holtman wrote: try this: b <- c(1, 0, 1, 0, 1, 0, 1, 0, 1, 0) p <- c(1, 3, 5, 7) b[p] <- ifelse(b[p] == 0, 1, 0) I'll have to look up the ifelse operator, looks like the ternary operator used in C b[p] = b[p]==0?1:0 Cool - thanks! Esmail __

Re: [R] flip certain bits in vector

2009-04-19 Thread Esmail
David Winsemius wrote: I do not think your wetware processed the inputs correctly. The second bit should not have been flipped: Ooops .. yes you are right! Try this loop free index based solution: b <- c( 1, 0, 1, 0, 1, 0, 1, 0, 1, 0) r <- b r[p] <- 0 + !r[p] # adding 0 converts logical

Re: [R] flip certain bits in vector

2009-04-19 Thread jim holtman
try this: > b <- c(1, 0, 1, 0, 1, 0, 1, 0, 1, 0) > p <- c(1, 3, 5, 7) > b[p] <- ifelse(b[p] == 0, 1, 0) > b [1] 0 0 0 0 0 0 0 0 1 0 On Sun, Apr 19, 2009 at 3:24 PM, Esmail wrote: > I have a string of binary values, and I would like to flip certain > bits in a set of positions. > > Let's say th

Re: [R] flip certain bits in vector

2009-04-19 Thread David Winsemius
I do not think your wetware processed the inputs correctly. The second bit should not have been flipped: Try this loop free index based solution: b <- c( 1, 0, 1, 0, 1, 0, 1, 0, 1, 0) r <- b r[p] <- 0 + !r[p] # adding 0 converts logical TRUE/FALSE to 0/1 r [1] 0 0 0 0 0 0 0 0 1 0 The "

[R] flip certain bits in vector

2009-04-19 Thread Esmail
I have a string of binary values, and I would like to flip certain bits in a set of positions. Let's say the vector p contains position [1, 3, 5, 7] vector b contains bits [1, 0, 1, 0, 1, 0, 1, 0, 1, 0] result r should be [0, 1, 0, 0, 0, 0, 0, 0, 1, 0] in pseudo code this would be somet