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

Gabor Grothendieck ggrothendieck at gmail.com
Sun Oct 21 15:24:36 CEST 2007


Try this:

A <- data.frame(subject = suba, a)
B <- data.frame(subject = subb, b)
merge(A, B, all = TRUE)[-1]

Omit all = TRUE if you only want matches, i.e. no rows with NAs, and
omit [-1] if you want an extra column giving the subject.

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.
>



More information about the R-help mailing list