[R] Matching multiple columns in a data frame

Bill.Venables at csiro.au Bill.Venables at csiro.au
Thu May 7 08:38:47 CEST 2009


> A[with(A, paste(C1, C2, sep = "\r")) %in% with(B, paste(C1, C2, sep="\r")), ]
   C1  C2
19  A 200 

(Using sep = "\r" is perhaps a little too cautious.)


Bill Venables
http://www.cmis.csiro.au/bill.venables/ 


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Raghavan, Nandini [PRDUS]
Sent: Thursday, 7 May 2009 4:19 PM
To: r-help at r-project.org
Subject: [R] Matching multiple columns in a data frame

Hello,

 

I am trying to extract a subset of a dataframe A (2 columns) by
extracting all entries in A (several repeated entries) that match
dataframe B in both columns.  For example, part of A and B are shown
below.  

The following does not seem to work correctly. This only seems to select
on the first component and all instances of the second.

ind <- A$C1 %in% B[,1] & A$C2 %in% B[,2] 

Any suggestions as to how to do this in general (even for matches in
multiple columns) would be appreciated.

 

Regards,

Nandini

 

 

A:

   C1   C2

1   F 1500

2   P  120

4   F  250

5   I  200

6   D 2010

7   F 1000

8   V    0

9   F 2100

10  F  500

11  E 1800

12  A  500

13  V    0

14  I  125

15  I   30

16  M  300

17  D   75

18  V  500

19  A  200

20  M 1000

21  P  225

 

B:

  C1   C2

1   A  200

2   A  600

3   A 1500

4   B  100

5   B 1000

6   C 5000

7   C  225

8   C  150

9   C  150

10  C  200

 

 

 

 

 

 


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