[R] Merging two data.frames

William Dunlap wdunlap at tibco.com
Mon Dec 8 22:47:38 CET 2014


Have you looked at the merge() function?

Here is an example.  I don't know if it resembles your problem.

  > M1 <- data.frame(V1=letters[1:3], V2=LETTERS[26:24], N1=101:103)
  > M2 <- data.frame(V1=letters[c(3,1,2,3,2)],
V2=LETTERS[c(23,26,22,24,24)],   N2=c(1003,1001,1002,1003,1002))
  > merge(M1,M2)
    V1 V2  N1   N2
  1  a  Z 101 1001
  2  c  X 103 1003
  > merge(M1, M2, all.x=TRUE)
    V1 V2  N1   N2
  1  a  Z 101 1001
  2  b  Y 102   NA
  3  c  X 103 1003
  > merge(M1, M2, all.y=TRUE)
    V1 V2  N1   N2
  1  a  Z 101 1001
  2  b  X  NA 1002
  3  b  V  NA 1002
  4  c  X 103 1003
  5  c  W  NA 1003



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Mon, Dec 8, 2014 at 8:42 AM, David Lambert <lambertd at ksu.edu> wrote:

> I have 2 data frames, M1[n,20] and M2[m,30].
>
>
> If V1 and V2 are the same in both M1 and M2, then append V3-V30 from M2
> onto M1.
>
>
> Otherwise, continue searching for a match.
>
>
> M1 is complete for all V1 and V2.  M2 is missing observations for V1 or
> V2, or both.
>
>
> I can't figure this one out, except with reference to my old fortran-era
> skills.  Help!
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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]]



More information about the R-help mailing list