0
x <- data.frame(k1 = c(NA,NA,3,4,5), a2 = c(1,NA,NA,4,5), dat = 1:5)
y <- data.frame(k1 = c(NA,2,NA,4,5), k2 = c(NA,NA,3,4,5), data = 1:5)
plyr::join(x,y,type='full')

Output of above script is:

  k1 a2 dat k2 data
1 NA  1   1 NA    1
2 NA  1   1  3    3
3 NA NA   2 NA    1
4 NA NA   2  3    3
5  3 NA   3 NA   NA
6  4  4   4  4    4
7  5  5   5  5    5
8  2 NA  NA NA    2

As I have no plyr package, I decide to use merge(x, y, all=TRUE,sort=FALSE) instead of plyr::join(x,y,type='full'),output as below:

> merge(x, y, all=TRUE,sort=FALSE)
  k1 a2 dat k2 data
1 NA  1   1 NA    1
2 NA  1   1  3    3
3 NA NA   2 NA    1
4 NA NA   2  3    3
5  4  4   4  4    4
6  5  5   5  5    5
7  3 NA   3 NA   NA
8  2 NA  NA NA    2

You can find different row order of row 6,7,8 between 2 methods.

As row order is critical to my project,how to use merge get the same result as plyr::join(x,y,type='full')?

kittygirl
  • 2,255
  • 5
  • 24
  • 52
  • 1
    Please read the duplicate link, which is basically your exact question. If you can't find what you're looking for there, then drop a comment and someone can reopen this question. – Tim Biegeleisen Apr 23 '19 at 16:39
  • 1
    If you have `dplyr`, then use `full_join(x, y)` – akrun Apr 23 '19 at 16:43
  • @akrun,you are right. I just cannot imagine R has so many similar functions. – kittygirl Apr 23 '19 at 16:49
  • @kittygirl `plyr` is kind of deprecated and gave way to `dplyr`. But, there are still some functions in `plyr` that doesn't have a replacement in `dplyr` – akrun Apr 23 '19 at 16:50

0 Answers0