Dear list,
I have a long list of two vectors with some matching elements. I would like to 
line them up in two columns and have an NA in those positions of the second 
vector where a match is absent. With a simple example, I will explain my 
problem.
(a<-1:6)
(b<-c(5,2))
(m1<-match(a,b))
(ab<-cbind(a,m1))
m2<-numeric(length(m1))
 for (i in 1:length(m1))
      {m2[i]<-ifelse(is.na(m1[i]),NA,b(m1[i]))}
# what I want to get - ab2 (shown below)
bsub<-c(NA,2,NA,NA,5,NA) # hoped to get this from m2 via the for loop above 
(non NA elements from the b vector)
(ab2<-cbind(a,bsub))

I get an error message that the function b is not found. How do I define this 
function?
Are there any other simpler methods for achieving my final result? Without a 
loop?
I did find one solution that almost solved my problem.
aa<-cbind(a,a)
bb<-cbind(b,b)
abm<-merge(aa,bb,by.x="a",by.y="b",all=TRUE)
abm[,2:3] # just what I want
However, if I choose a little more complicated version of the problem :

rm(list=ls())
(a<-c(8,5,4,7,3,4,5,2,1))
(b<-c(5,2))
(a1<-sort(a))
(b1<-sort(b))
aa<-cbind(a1,a1)
bb<-cbind(b1,b1)
(ab<-merge(aa,bb,by.x="a1",by.y="b1",all=TRUE))
ab[,2:3]
In this case, there is a match for both the 5's in a1. I want only one match as 
there is only one 5 in b.
My question is - is there any generalised solution for the above problem?
One extra feature that would be interesting is if there are elements in the 2nd 
vector that are missing in the 1st.
In that case, I would like to have NA's in the 1st vector matching the extra 
elements in the 2nd vector.

I will appreciate any help that I can get.
Thanks,
Ravi


______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to