[R] merge without NA last

arun smartpink111 at yahoo.com
Fri May 31 14:30:02 CEST 2013

 colnames(Y)[2]<- colnames(X)
#    k1 k2
#1    A  1
#2 <NA> NA
#3    C  3
#4    B  2

----- Original Message -----
From: nevil amos <nevil.amos at gmail.com>
To: r-help <R-help at r-project.org>
Sent: Friday, May 31, 2013 4:07 AM
Subject: [R] merge without NA last

I am trying to create a merge where the first value contains NA values.  I
wish to retain these in order.  however when I use all.x=T and sort=F they
are retained but na values are placed last:

> X<-data.frame(k1=c("A",NA,"C","B"))
> print (X)
1    A
2 <NA>
3    C
4    B
> Y<-data.frame(k2=c(1,2,3),k3=c("A","B","C"))
> Z<-merge(X,Y, by.x=1,by.y=2,all.x=T,sort=F)
> print (Z)
    k1 k2
1    A  1
2    C  3
3    B  2
4 <NA> NA

The result I need is
    k1  k2
1    A  1
2 <NA> NA
3    C 3
4    B 2

how do I prevent NA c=values being sorted to last - I need to retain values
in position.


Nevil Amos

    [[alternative HTML version deleted]]

R-help at r-project.org mailing list
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