Learn R Programming

Kmisc (version 0.5.0)

pMerge: Merge a Data Frame 'into' Another

Description

This function will merge a data frame df2 'into' a data frame df1, preserving df1 as much as possible in the merger. Hence I call this a 'preserving' merge, or pMerge.

Usage

pMerge(df1, df2, by = intersect(names(df1), names(df2)), doCheck = FALSE)

Arguments

df1
the data.frame which we are preserving
df2
the data.frame we are merging into df1
by
character; name of the variable we are merging over
doCheck
boolean; set this if you want to perform more extensive (but slower) error checking

Examples

Run this code
df1 <- data.frame( stringsAsFactors=FALSE,
  x=1:1000,
  y=sample(LETTERS, size=1000, replace=TRUE)
)

df2 <- data.frame( stringsAsFactors=FALSE,
  x=sample( 1:2000, size=2000, replace=TRUE ),
  z=sample( letters, size=2000, replace=TRUE ),
  q=sample( LETTERS, size=2000, replace=TRUE )
)

dMerged <- pMerge( df1, df2, by="x" )
stopifnot( all.equal( df1, dMerged[1:ncol(df1)] ) )

Run the code above in your browser using DataLab