[R] help in matching two column vectors

jim holtman jholtman at gmail.com
Sat Sep 12 02:37:14 CEST 2009


Is this what you want:

> (a<-1:6)
[1] 1 2 3 4 5 6
> (b<-c(5,2))
[1] 5 2
> (m1<-match(a,b))
[1] NA  2 NA NA  1 NA
> cbind(a, b[m1])
     a
[1,] 1 NA
[2,] 2  2
[3,] 3 NA
[4,] 4 NA
[5,] 5  5
[6,] 6 NA
>

Your 'missing function b' in the first example was due to you thinking
'b(m1[i])' is equivalent to 'b[m1[i]]'; the first is a function call.
On Fri, Sep 11, 2009 at 5:00 PM, ravi <rv15i at yahoo.se> wrote:
> 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 at 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list