andandandand
andandandand

Reputation: 22270

R - negate values at specified positions in a integer vector

I want to apply logical NOT to the specified indexes of a binary integer vector.

Example: given c(0, 1, 0) and c(1, 3), I want to obtain c(1, 1, 1).

What's a vectorized way to do this?

Upvotes: 0

Views: 49

Answers (1)

G5W
G5W

Reputation: 37641

x = c(0, 1, 0) 
Changes = c(1, 3)
x[Changes] = !x[Changes]
x
[1] 1 1 1

Upvotes: 1

Related Questions