Reputation: 916
I have two numeric vectors:
a <- c(1,2,3,4,5,6,7,8)
b <- c(4,2,2,3,9,10,7,7,10,14)
I want to set any number in b
that does not appear in a
to zero.
My desired result is:
c <- c(4,2,2,3,0,0,7,7,0,0)
who can I do this in an elegant way?
(I was thinking to use left_join but I think there must be some more elegant approach)
Upvotes: 2
Views: 62
Reputation: 11981
You can do this by subsetting b
with the %in%
function:
b[! b %in% a] <- 0
Upvotes: 2