I have a named vector (say n) with names as consecutive integers from 1 to 25000, and values associated with it, and another smaller vector (say m), similar to "n", with 21399 elements, with the names starting at 1 and ending at 25000.
I want to add named elements to the vector "m" for the missing names and assign a value of 0 to them.
I did that by creating two data frames with names and values as two columns for both and then doing a left join, but are there any better ways of doing it?
A much smaller example:
> n=c("1"=3,"2"=5,"3"=2,"4"=9,"5"=8)
> n
1 2 3 4 5
3 5 2 9 8
> m=c("1"=9,"2"=7,"5"=2)
> m
1 2 5
9 7 2
I want my final "m" to be:
> m
1 2 3 4 5
9 7 0 0 2
What I did was:
> x=data.frame("p"=names(n),n)
> y=data.frame("p"=names(m),m)
> join(x,y,by="p")
p n m
1 1 3 9
2 2 5 7
3 3 2 0
4 4 9 0
5 5 8 2