mergeAttr {eatModel}R Documentation

Merge Two Data Frames and maintain variable attributes

Description

This is a wrapper for the merge function from the base package. merge does not maintain variable attributes. mergeAttr might be useful if variable attributes should be maintained.

Usage

mergeAttr(x, y, by = intersect(names(x), names(y)),
      by.x = by, by.y = by, all = FALSE, all.x = all, all.y = all,
      sort = TRUE, suffixes = c(".x",".y"))

Arguments

x

first data frame to be merged.

y

second data frame to be merged.

by

specifications of the columns used for merging

by.x

specifications of the columns used for merging

by.y

specifications of the columns used for merging

all

logical; all = L is shorthand for all.x = L and all.y = L, where L is either TRUE or FALSE.

all.x

logical; if TRUE, then extra rows will be added to the output, one for each row in x that has no matching row in y. These rows will have NAs in those columns that are usually filled with values from y. The default is FALSE, so that only rows with data from both x and y are included in the output.

all.y

logical; analogous to all.x.

sort

logical. Should the result be sorted on the by columns?

suffixes

a character vector of length 2 specifying the suffixes to be used for making unique the names of columns in the result which not used for merging (appearing in by etc).

Value

data frame. See the help page of merge for further details.

Author(s)

Sebastian Weirich

Examples

### data frame 1, variable 'y' with variable.label 'test participation'
df1 <- data.frame ( id = 1:3, sex = factor ( c("male", "male", "female")), y = c(TRUE,FALSE,FALSE))
attr(df1[,"y"], "variable.label") <- "test participation"

### data frame 2 without labels 
df2 <- data.frame ( id = c(2,4), status = factor ( c("married", "single")), z = c(TRUE,FALSE))

### lost label after merging
df3 <- merge(df1, df2, all = TRUE)
attr(df3[,"y"], "variable.label")

### maintain label
df4 <- mergeAttr(df1, df2, all = TRUE)
attr(df4[,"y"], "variable.label")

[Package eatModel version 0.6.11 Index]