Learn R Programming

sjmisc (version 2.6.3)

merge_df: Merge labelled data frames

Description

Merges (full join) two (or more) data frames and preserve value and variable labels.

Usage

merge_df(x1, x2, ..., id = NULL)

Arguments

x1

First data frame to be merged.

x2

Second data frame to be merged.

...

More data frames to be merged.

id

Optional name for ID column that will be created to indicate the source data frames for appended rows.

Value

A full joined data frame.

Details

This function merges two data frames, where equal named columns will be joined together. Matching rows are not omitted, hence all rows from the data frames are preserved. This means that merge_df row-binds all data frames, even if these have different numbers of columns. Non-matching columns will be column-bound and filled with NA-values for rows in those data frames that do not have this column.

Value and variable labels are preserved. If matching columns have different value label attributes, attributes from first data frame will be used.

Examples

Run this code
# NOT RUN {
library(dplyr)
data(efc)
x1 <- efc %>% select(1:5) %>% slice(1:10)
x2 <- efc %>% select(3:7) %>% slice(11:20)

mydf <- merge_df(x1, x2)
mydf
str(mydf)

# }
# NOT RUN {
library(sjPlot)
view_df(mydf)
# }
# NOT RUN {
x3 <- efc %>% select(5:9) %>% slice(21:30)
x4 <- efc %>% select(11:14) %>% slice(31:40)

mydf <- merge_df(x1, x2, x3, x4, id = "subsets")
mydf
str(mydf)

# }

Run the code above in your browser using DataLab