Learn R Programming

sjmisc (version 2.8.10)

add_rows: Merge labelled data frames

Description

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

Usage

add_rows(..., id = NULL)

merge_df(..., id = NULL)

Value

A full joined data frame.

Arguments

...

Two or 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.

Details

This function works like dplyr::bind_rows(), but preserves variable and value label attributes. add_rows() row-binds all data frames in ..., 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.

merge_df() is an alias for add_rows().

Examples

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

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

if (FALSE) {
library(sjPlot)
view_df(mydf)}

x3 <- efc %>% select(5:9) %>% slice(21:30)
x4 <- efc %>% select(11:14) %>% slice(31:40)

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

Run the code above in your browser using DataLab