Bill.Venables at csiro.au
2007-Jul-09 11:49 UTC
[R] a little problem on selecting a subset from dataset A accordingto dataset B?
> AB <- with(B, subset(A, coords.x1 %in% X1)) > ABcoords.x1 coords.x2 0 542250.9 3392404 7 541512.5 3394722 8 541479.3 3394878 9 538903.4 3395943 18 543274.0 3389919 19 543840.8 3392012 -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of zhijie zhang Sent: Monday, 9 July 2007 2:43 AM To: R-help at stat.math.ethz.ch Subject: [R] a little problem on selecting a subset from dataset A accordingto dataset B? 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. The folloing shows dataset A and B.> Acoords.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> BX1 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 -- With Kind Regards, oooO::::::::: (..)::::::::: :\.(:::Oooo:: ::\_)::(..):: :::::::)./::: ::::::(_/:::: ::::::::::::: [*********************************************************************** ] Zhi Jie,Zhang ,PHD Tel:86-21-54237149 Dept. of Epidemiology,School of Public Health,Fudan University Address:No. 138 Yi Xue Yuan Road,Shanghai,China Postcode:200032 Email:epistat at gmail.com Website: statABC.com [*********************************************************************** ] oooO::::::::: (..)::::::::: :\.(:::Oooo:: ::\_)::(..):: :::::::)./::: ::::::(_/:::: ::::::::::::: [[alternative HTML version deleted]] ______________________________________________ R-help at stat.math.ethz.ch mailing list stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
jim holtman
2007-Jul-09 19:33 UTC
[R] a little problem on selecting a subset from dataset A accordingto dataset B?
You might want to be careful since what you are comparing is floating point numbers. You might want to scale them and then convert to integers to make sure that you are getting the numbers you think you should be getting. (FAQ 7.31) On 7/9/07, Bill.Venables at csiro.au <Bill.Venables at csiro.au> wrote:> > AB <- with(B, subset(A, coords.x1 %in% X1)) > > AB > coords.x1 coords.x2 > 0 542250.9 3392404 > 7 541512.5 3394722 > 8 541479.3 3394878 > 9 538903.4 3395943 > 18 543274.0 3389919 > 19 543840.8 3392012 > > > > -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of zhijie zhang > Sent: Monday, 9 July 2007 2:43 AM > To: R-help at stat.math.ethz.ch > Subject: [R] a little problem on selecting a subset from dataset A > accordingto dataset B? > > 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. > 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 > > -- > With Kind Regards, > > oooO::::::::: > (..)::::::::: > :\.(:::Oooo:: > ::\_)::(..):: > :::::::)./::: > ::::::(_/:::: > ::::::::::::: > [*********************************************************************** > ] > Zhi Jie,Zhang ,PHD > Tel:86-21-54237149 > Dept. of Epidemiology,School of Public Health,Fudan University > Address:No. 138 Yi Xue Yuan Road,Shanghai,China > Postcode:200032 > Email:epistat at gmail.com > Website: statABC.com > [*********************************************************************** > ] > oooO::::::::: > (..)::::::::: > :\.(:::Oooo:: > ::\_)::(..):: > :::::::)./::: > ::::::(_/:::: > ::::::::::::: > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide 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?