Tapajit Dey Tapajit Dey - 2 months ago 6
R Question

How to add named elements to a vector in R

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

Answer

A couple of attempts:

m[setdiff(names(n), names(m))] <- 0
m[order(as.numeric(names(m)))]
#1 2 3 4 5 
#9 7 0 0 2 

m <- c(m,replace(n,,0))
tapply(m, names(m), sum)
#1 2 3 4 5 
#9 7 0 0 2

replace(replace(n,,0), match(names(m),names(n)), m)
#1 2 3 4 5 
#9 7 0 0 2

With NAs instead of 0's:

setNames( m[match(names(n),names(m))], names(n) )
# 1  2  3  4  5 
# 9  7 NA NA  2