[R] Merging two data set in R,

Sarah Goslee sarah.goslee at gmail.com
Wed Aug 25 14:42:22 CEST 2010


Almost. You'll need to handle the duplicate ID yourself since R has
no way of knowing which one(s) to change to NA. As I already suggested, you
can use unique() in conjunction with whatever logical rules you require
for choosing those values.

As I also already suggested, all.y and all.x are the options to merge()
that you need to consider.

> A <- data.frame(ID = c(1,2,3), X = c('a','b','c'))
> B <- data.frame(ID = c(1,2,2), Y = c('x','y','z'))
> merge(A, B, all.x=FALSE, all.y=TRUE)
  ID X Y
1  1 a x
2  2 b y
3  2 b z

Just think how much easier this process would have been if you had
provided a clear question with toy data and examples of what you'd
tried in your first question.

Sarah

On Wed, Aug 25, 2010 at 8:24 AM, Mangalani Peter Makananisa
<pmakananisa at sars.gov.za> wrote:
> A:
> ID X
> 1  a
> 2  b
> 3  c
>
>
> B:
> ID Y
> 1  x
> 2  y
> 2  z
>
> I would like to see something like this:
>
> Common = Merge(A,B)
> Common
>
> ID  X    Y
> 1   a    x
> 2   b    y
> 2  N/A   z
>
> If it is possible,
>
>


-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list