[R] finding paired values from common subjects -- vector operation?

jim holtman jholtman at gmail.com
Sun Oct 21 15:15:59 CEST 2007


Here is one way of doing it:

> a=c(100,200,150,120,140,180)
>
> b=c(200,300,420,130)
>
> suba=c(1,2,3,4,5,6)
>
>
> subb=c(1,3,5,6)
>
> pairs <- match(suba, subb, nomatch=0)
> # create matrix of matching pairs
> cbind(a[pairs != 0], b[pairs])
     [,1] [,2]
[1,]  100  200
[2,]  150  300
[3,]  140  420
[4,]  180  130


On 10/21/07, gallon li <gallon.li at gmail.com> wrote:
> I have two vectors for values collected from a group of subjects, say
>
> a=c(100,200,150,120,140,180)
>
> b=c(200,300,420,130)
>
> I also have two vectors which indicate the corresponding subjects for a and
> b, say
>
> for a, the subjects are
>
> suba=c(1,2,3,4,5,6)
>
> for b, the subjects are
>
> subb=c(1,3,5,6)
>
> Then, I want to find out the paired values from a and b, such as (100, 200)
> (from subject 1), (150,300) (from subject 3), (140, 420) (from subject 5)
> .....
>
> How can I do this in R? Can somebody drop a hint here?
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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 you are trying to solve?



More information about the R-help mailing list