merge without NA last

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

merge without NA last

Nevil Amos
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)
    k1
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.

thanks

Nevil Amos

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: merge without NA last

Milan Bouchet-Valat
Le vendredi 31 mai 2013 à 18:07 +1000, nevil amos a écrit :

> 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)
>     k1
> 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.
merge(..., sort=FALSE) does not guarantee the order will be preserved,
even when NAs are not present. If you want to preserve the order, you
can add a sequence number to the original data frame:
X <- data.frame(k1=c("A",NA,"C","B"), id=seq.int(4))
and sort manually on that column after the merge.


Regards

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: merge without NA last

arun kirshna
In reply to this post by Nevil Amos
library(plyr)
 colnames(Y)[2]<- colnames(X)
 join(X,Y,type="left",by="k1")
#    k1 k2
#1    A  1
#2 <NA> NA
#3    C  3
#4    B  2
A.K.



----- Original Message -----
From: nevil amos <[hidden email]>
To: r-help <[hidden email]>
Cc:
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)
    k1
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.

thanks

Nevil Amos

    [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.