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 "if" statement is for control but you might have succeeded with an ifelse function which is generally more useful for conditional modification of vectors. It could have been used if the transformation were not binary.

--
David Winsemius

On 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 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 something like

---

r = c()

for (i in 1:10)
 if (i in p)
    r = c(r, flip[i])

r
----

doesn't work :-)  R doesn't like the if statement.

Is there a nice, concise way to do this? The vector contents and
size will vary, but length of p <= b.

Thanks,
Esmail

______________________________________________
R-help@r-project.org mailing list
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.

Reply via email to