You'll have to do this in two stages.  The merge is just a case of
specifying all=TRUE:

data1 <- data.frame(Code=c(2,6,7,17), Cap04=c(120,75,220,4))
data2 <- data.frame(Code=c(2,7,9,17), Cap08=c(120,112,190,4))

data.merge <- merge(data1,data2, all=TRUE)
data.merge
  Code Cap04 Cap08
1    2   120   120
2    6    75    NA
3    7   220   112
4    9    NA   190
5   17     4     4

Now you'll need to convert the NAs to zeros.

data.merge[is.na(data.merge)] <- 0
data.merge

  Code Cap04 Cap08
1    2   120   120
2    6    75     0
3    7   220   112
4    9     0   190
5   17     4     4

Hope this helps.

Dave


David Barron
51 Stratfield Road
Oxford OX2 7BG
01865 512938
07595 090381


On 24 March 2014 18:47, Megan Weigel <mw5w...@gmail.com> wrote:

> Hello,
>
> I need to combine two data sets into one. For example:
>
> Dataset1:
> Code   Cap04
> 2        120
> 6          75
> 7        220
> 17         4
>
> Dataset2:
> Code   Cap08
> 2        120
> 7        112
> 9        190
> 17         4
>
>
> I need the dataset to look like the following where it keeps every unique
> 'code' from both datasets and fills the blanks in both rows with 0:
>
> FinalDataSet:
> Code   Cap04   Cap08
> 2         120       120
> 6          75          0
> 7         220       112
> 9            0       190
> 17          4          4
>
> Can anyone help me with this please?
>
> Thank you very much!
> Johnson
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to