mergeAttr: Merge Two Data Frames and maintain variable attributes

Description Usage Arguments Value Author(s) Examples

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

1
2
3
4
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"), setAttr = TRUE, onlyVarValLabs = TRUE,
      homoClass = TRUE)

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).

setAttr

Logical: restore the variable attributes? If FALSE, the behavior of mergeAttr equals the behavior of merge.

onlyVarValLabs

Logical: If TRUE, only the variable and value labels will be restored. If FALSE, all variable attributes will be restored.

homoClass

Logical: Beginning with R version 3.5, merge may give an error if the class of the by-variables differs in both data.frames. If TRUE, class of by-variable(s) will be homogenized before merging.

Value

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

Author(s)

Sebastian Weirich

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
### 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, onlyVarValLabs = FALSE)
attr(df4[,"y"], "variable.label")

eatModel documentation built on May 2, 2019, 6:49 p.m.