[R] merge 2 data.frames in dependence of 2 values

Rui Barradas ruipbarradas at sapo.pt
Thu Nov 13 18:45:58 CET 2014


Hello,

See ?merge, in particular the argument 'all'.

dat1 <- read.table(text = "
         NAME    MONTH     BONUS
1      Andy     2014-10       100
2      Pete     2014-10        200
3      Marc    2014-10        300
4      Andy    2014-11        400
", header = TRUE, stringsAsFactors = FALSE)


dat2 <- read.table(text = "
       NAME  MONTH    BONUS_2
1    Andy    2014-10       150
2    Pete     2014-11       180
3    Jason   2014-10       190
4    Paul     2014-10       210
5    Andy    2014-11       30
", header = TRUE, stringsAsFactors = FALSE)

merge(dat1, dat2, all = TRUE)


Hope this helps,

Rui Barradas

Em 13-11-2014 14:02, Matthias Weber escreveu:
> Hello togehter,
>
> i have a little problem. Maybe anyone can help me.
>
> I have 2 data.frames, which look like as follows:
> First:
>
>          NAME    MONTH     BONUS
> 1      Andy     2014-10       100
> 2      Pete     2014-10        200
> 3      Marc    2014-10        300
> 4      Andy    2014-11        400
>
> Second:
>
>        NAME  MONTH    BONUS_2
> 1    Andy    2014-10       150
> 2    Pete     2014-11       180
> 3    Jason   2014-10       190
> 4    Paul     2014-10       210
> 5    Andy    2014-11       30
>
> How can I merge this 2 data.frames, if I want the following result:
>
>          NAME    MONTH     BONUS    BONUS_2
> 1     Andy     2014-10       100            150
> 2     Pete     2014-11                         180
> 3     Marc    2014-10        300
> 4     Andy    2014-11       400             30
> 5     Pete     2014-10       200
> 6     Jason  2014-10                         190
> 7     Paul     2014-10                         210
>
> The important thing is, that for every accordance in the Columns "NAME" and "MONTH" I get a new line.
>
> Thanks for your help.
>
> Best regards.
>
> Mat
>
> ________________________________
> This e-mail may contain trade secrets, privileged, undisclosed or otherwise confidential information. If you have received this e-mail in error, you are hereby notified that any review, copying or distribution of it is strictly prohibited. Please inform us immediately and destroy the original transmittal. Thank you for your cooperation.
>
> Diese E-Mail kann Betriebs- oder Geschaeftsgeheimnisse oder sonstige vertrauliche Informationen enthalten. Sollten Sie diese E-Mail irrtuemlich erhalten haben, ist Ihnen eine Kenntnisnahme des Inhalts, eine Vervielfaeltigung oder Weitergabe der E-Mail ausdruecklich untersagt. Bitte benachrichtigen Sie uns und vernichten Sie die empfangene E-Mail. Vielen Dank.
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>



More information about the R-help mailing list