[R] a little problem on selecting a subset from dataset A according to dataset B?

Chuck Cleland ccleland at optonline.net
Mon Jul 9 12:20:33 CEST 2007


zhijie zhang wrote:
> Dear Friends,
>    I want to extract the records from A according to B, but the results are
> not correct because R says :
>   The length of long object is not integer times on the length of short
> object.
>   Anybody have met the same problem? How to do it correctly?
> 
> length(A)=47
> length(B)=6
> 
> A[A$coords.x1==B$X1,]   #the program for the above task. I should get 6
> records, but i only get former 4 records for the above reason.
> 
> Thanks.

A[A$coords.x1 %in% B$X1,]
   coords.x1 coords.x2
1   542250.9   3392404
8   541512.5   3394722
9   541479.3   3394878
10  538903.4   3395943
19  543274.0   3389919
20  543840.8   3392012

?is.element

>  The folloing shows dataset A and B.
> 
> 
>> A
>    coords.x1 coords.x2
> 0  542250.89 3392404.1
> 1  538813.87 3388339.0
> 2  536049.19 3385821.6
> 3  533659.62 3383194.2
> 4  530642.30 3376834.9
> 5  529573.15 3378177.8
> 6  530853.82 3394838.8
> 7  541512.51 3394721.6
> 8  541479.33 3394877.8
> 9  538903.39 3395942.5
> 10 536019.95 3396286.1
> 11 538675.23 3384213.2
> 12 535127.95 3381255.4
> 13 533852.24 3378660.4
> 14 531360.91 3379273.8
> 15 539289.14 3375759.8
> 16 543410.51 3384353.1
> 17 543089.27 3388170.1
> 18 543274.03 3389919.2
> 19 543840.77 3392012.4
> 20 553383.55 3402401.8
> 21 554621.51 3397938.9
> 22 564096.42 3397524.4
> 23 567529.64 3398702.9
> 24 561798.76 3404864.0
> 25 562868.34 3405502.2
> 26 563145.22 3403192.1
> 27 562419.87 3404090.4
> 28 558321.85 3403879.9
> 29 567050.74 3404973.1
> 30 570609.70 3408742.4
> 31 556777.57 3397858.0
> 32 531353.38 3368596.6
> 33 533513.50 3372749.3
> 34 537543.19 3364284.8
> 35 538779.41 3368224.8
> 36 525930.09 3374067.7
> 37 522990.85 3369213.1
> 38 528826.37 3359019.0
> 39 533865.85 3362595.4
> 40 531200.25 3365053.0
> 41 551054.10 3377181.3
> 42 546974.19 3369284.8
> 43 572315.59 3359541.1
> 44 562703.63 3355173.4
> 45 558959.31 3357804.4
> 46 558531.39 3361741.1
> 
> 
>> B
>          X1        X2
> 1 542250.89 3392404.1
> 2 541512.51 3394721.6
> 3 541479.33 3394877.8
> 4 538903.39 3395942.5
> 5 543274.03 3389919.2
> 6 543840.77 3392012.4

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list