gallon li
2007-Oct-21 06:50 UTC
[R] finding paired values from common subjects -- vector operation?
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]]
jim holtman
2007-Oct-21 13:15 UTC
[R] finding paired values from common subjects -- vector operation?
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?
Gabor Grothendieck
2007-Oct-21 13:24 UTC
[R] finding paired values from common subjects -- vector operation?
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. >