Re: [R] conditional matching of rows of tables

From: jim holtman <jholtman_at_gmail.com>
Date: Thu, 20 Mar 2008 20:19:54 -0500

This should do it for you:

> A

  V1 V2 V3 V4
1 1 a 0 4
2 1 b 5 8
3 2 a 0 3
4 2 b 4 7
> B

  V1 V2 V3
1 1 2 3
2 2 4 5
3 2 0 3
4 1 7 8
> B1 <- B # create a copy and add a new column
> B1$key <- ""
> for (i in seq(nrow(B))){

+     indx <- which((B[i,1] == A[,1]) & (B[i,2] >= A[,3]) & (B[i,3] <= A[,4]))
+     if (length(indx) == 0){
+         warning("No match for row:", i)
+         next
+     }
+     if (length(indx) > 1) warning("multiple matches for row:", i)
+     B1$key[i] <- as.character(A$V2[indx[1]])  # take first match if multiples
+ }

>
> B1

  V1 V2 V3 key
1 1 2 3 a
2 2 4 5 b
3 2 0 3 a
4 1 7 8 b
>

On Thu, Mar 20, 2008 at 7:00 PM, Stanley Ng <stanleyngkl_at_gmail.com> wrote:
> Second try :P
>
> I have matrix A of 4 cols:
> 1 a 0 4
> 1 b 5 8
> 2 a 0 3
> 2 b 4 7
>
> And matrix B of 3 cols:
> 1 2 3
> 2 4 5
> 2 0 3
> 1 7 8
>
> I would like to assign (a or b) to the rows of matrix B. The rules are that
> in each row of matrix B, the 1st value must match the 1st col. of matrix A,
> 2nd and 3rd values must lie between 3rd and 4rd cols (inclusive) of matrix
> A.
>
> For example, the 1st row of matrix B is 1 2 3, the 1st value "1" corresponds
> to 1st and 2nd row of matrix A. Next, its 2nd and 3rd values "2" and "3" lie
> between "0" and "4" of 1st row of matrix A. Thus 1st row of matrix B is
> assigned "a". Similarly, the assignments for remaining rows of matrix B are
> 2 4 5 -> "b"
> 2 0 3 -> "a"
> 1 7 8 -> "b"
>
>
>
> -----Original Message-----
> From: jim holtman [mailto:jholtman_at_gmail.com]
> Sent: Friday, March 21, 2008 00:33
> To: Ng Stanley
> Cc: r-help
> Subject: Re: [R] conditional matching of rows of tables
>
> Not exactly clear on the transformation that you want to do. In your
> example, '1 2 3 -> a', where does the '2 3' come from since I don't see a
> value of 2 in the 3rd & 4th columns. So a better explanation of what you
> are trying to do would be help and show where the values came from in each
> case.
>
> On 3/20/08, Ng Stanley <stanleyngkl_at_gmail.com> wrote:
> > Hi,
> >
> > Given matrix A of 4 cols.
> >
> > 1 a 0 4
> > 1 b 5 8
> > 2 a 0 3
> > 2 b 4 7
> >
> > I have another matrix B of 3 cols. How to assign (a or b) to the rows
> > such that in each row its 1st value must match the 1st col. of A, 2nd
> > and 3rd values must lie between 3rd and 4rd cols (inclusive) of A
> >
> > 1 2 3 -> a
> > 2 4 5 -> b
> > 2 0 3 -> a
> > 1 7 8 -> b
> >
> > [[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?
>
> ______________________________________________
> 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?

______________________________________________
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.
Received on Fri 21 Mar 2008 - 01:23:10 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 21 Mar 2008 - 03:30:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive