[R] Merging two data frames, but keeping NAs

Rainer M Krug Rainer at krugs.de
Fri Dec 6 09:35:57 CET 2013


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



On 12/05/13, 16:37 , arun wrote:
> Hi, Try ?join()
> 
> library(plyr)

Well - what would we do without Hadley ...

He solved many problems we didn't know we would have soon...

Cheers,

Rainer

> y$ref <- y$id
>> join(x,y,by="ref")
> ref id val 1   NA NA  NA 2   NA NA  NA 3   NA NA  NA 4   10 NA  NA 
> 5    9  9 106 6    8  8 107 7    7  7 105 8    6  6 104 9    5 NA
> NA 10  NA NA  NA 11   1 NA  NA 12   2  2 101 13   3  3 102 14   4
> 4 103 15   5 NA  NA
> 
> 
> A.K.
> 
> 
> On Thursday, December 5, 2013 9:58 AM, Rainer M Krug
> <Rainer at krugs.de> wrote: Hi
> 
> My brain is giving up on this...
> 
> I have the following two data.frames:
> 
> x <-  data.frame(ref=c(NA, NA, NA, 10:5, NA, 1:5)) y <-
> data.frame(id = c(2, 3, 4, 6, 7, 9, 8), val = 101:107)
> 
> Which look as follow:
> 
>> x
> ref 1   NA 2   NA 3   NA 4   10 5    9 6    8 7    7 8    6 9    5 
> 10  NA 11   1 12   2 13   3 14   4 15   5
>> y
> id val 1  2 101 2  3 102 3  4 103 4  6 104 5  7 105 6  9 106 7  8
> 107
> 
> 
> Now I want to merge y into x, but that
> 
> a) the sort order of x stays the same (sort=FALSE in merge()) and 
> b) the NAs stay
> 
> The result should look as follow (column id only here for
> clarity):
> 
>> result
> ref  id  val 1   NA  NA  NA 2   NA  NA  NA 3   NA  NA  NA 4   10
> NA  NA 5    9   9   106 6    8   8   107 7    7   7   105 8    6
> 6   104 9    5  NA  NA 10  NA  NA  NA 11   1  NA  NA 12   2   2
> 101 13   3   3  102 14   4   4  103 15   5  NA  NA
> 
> merge(x, y, by.x="ref", by.y="id", sort=FALSE) leaves out the NA,
> but otherwise it works:
> 
>> merge(x, y, by.x=1, by.y="id", sort=FALSE)
> ref val 1   9 106 2   8 107 3   7 105 4   6 104 5   2 101 6   3
> 102 7   4 103
> 
> Is there any way that I can tell merge() to keep the NA, or how can
> I achieve what I want?
> 
> Thanks,
> 
> Rainer
> 
> 
> ______________________________________________ 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.
> 

- -- 
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Stellenbosch University
South Africa

Tel :       +33 - (0)9 53 10 27 44
Cell:       +33 - (0)6 85 62 59 98
Fax :       +33 - (0)9 58 10 27 44

Fax (D):    +49 - (0)3 21 21 25 22 44

email:      Rainer at krugs.de

Skype:      RMkrug
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.22 (Darwin)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJSoYxtAAoJENvXNx4PUvmC8JMIANWUXBhCFgKv+wZs2oKv1jMm
qGLcd31a55j8NSoZZRf5v6coG+UEdVGhBu4cLlt1+0BRAhYIK9AnLvV9KXbt5zbI
PKySevB3box1ILbwsr8JH2YyOtlgjjint4LcGuEr4doNy0uo7a3G9J3ctxZgDFeE
QrmDH8EFc55lX76gzp41xUaAxvBP72GlgwK9O4jyO4f19LFcJ87C68s7Gwm2Qs4x
Ysc3JmZ8tC4BlD4H5FV/Pf6cLCxoX3CgQERGD+NNe5HCW/XSXOYsKzreamPr7ayd
bAuTDLRpPqUSYKG/nbcvjj0HMs06YNTYP4LTnwp08QUJ2VH98viQkTBF8OxDGgI=
=mK8w
-----END PGP SIGNATURE-----



More information about the R-help mailing list