Supposed I have a vector:
v <- c(a=1, b=2, c=3, d=4)
v
a b c d
1 2 3 4
I would like to create a new vector, new_v, such that: if an element can be found in v, it equals to the value in v, otherwise assign to 0. For example, e, f, and g don't exist in v and they all are assigned as 0, such that:
> new_v
a b c d e f g
1 2 3 4 0 0 0
I know it can be created by ifelse for each eletment. Since I have a long vector, I am wondering what the best way to create it.