Skip to content

Finding rows common to two datasets

6 messages · Steve Murray, Umesh Srinivasan, jim holtman +1 more

#
Dear all,
 
I have 2 data frames, both with 14 columns of data and differing numbers of rows. The first two columns are 'Latitude' and 'Longitude'. I want to find the pairs of Latitude and Longitude coordinates which are common to both datasets, and output a new data frame which is composed of these coincident rows. I tried using the 'unique' command, but had difficulties interpreting the help file.
 
Many thanks for any help offered,
 
Steve
#
Thanks for the reply, however, when I do the following command, I receive the message: 'data frame with 0 columns and 0 rows'. I've checked again though, and there should be several thousand rows where the Latitude and Longitude pairs are the same.
data frame with 0 columns and 0 rows


Is there an obvious solution to this? Should I be using 'unique' instead, and if so, how would I get the above to correspond to this command?

Thanks,

Steve




________________________________
#
You are missing a comma:

common <- intersect(data_frame_x[,c("Latitude", "Longitude")],
data_frame_y[,c("Latitude","Longitude")])
On Tue, Apr 28, 2009 at 5:49 AM, Steve Murray <smurray444 at hotmail.com> wrote:

  
    
#
I think merge() can do what's wanted, but you do have to be careful that values match exactly.  Here's an example where two data frames print the same in a row for columns 'a' and 'b', but are not exactly same.  merge() returns zero rows.  This problem can be fixed in this case by rounding, but that's not a good general solution because very close numbers can round to different numbers, e.g., 1.499 and 1.501.

Here are examples:
a b c
1 1 3 A
2 2 4 B
a b c
1 1 3 C
2 2 5 D
[1] a   b   c.x c.y
<0 rows> (or 0-length row.names)
a b c.x c.y
1 1 3   A   C
c
1 C
2 D
a c
1 1 C
2 2 D
-- Tony Plate
jim holtman wrote: