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

add_rows(..., id = NULL)

merge_df(..., id = NULL)

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.

Value

A full joined data frame.

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

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
#>    c12hour e15relat e16sex e17age e42dep c82cop1 c83cop2
#> 1       16        2      2     83      3      NA      NA
#> 2      148        2      2     88      3      NA      NA
#> 3       70        1      2     82      3      NA      NA
#> 4      168        1      2     67      4      NA      NA
#> 5      168        2      2     84      4      NA      NA
#> 6       16        2      2     85      4      NA      NA
#> 7      161        1      1     74      4      NA      NA
#> 8      110        4      2     87      4      NA      NA
#> 9       28        2      2     79      4      NA      NA
#> 10      40        2      2     83      4      NA      NA
#> 11      NA       NA      1     68      4       3       4
#> 12      NA       NA      2     97      3       3       3
#> 13      NA       NA      2     80      4       3       2
#> 14      NA       NA      2     75      3       3       2
#> 15      NA       NA      2     82      3       2       3
#> 16      NA       NA      2     89      3       4       2
#> 17      NA       NA      1     80      1       3       2
#> 18      NA       NA      1     72      3       4       2
#> 19      NA       NA      1     94      3       3       2
#> 20      NA       NA      1     79      4       3       2
str(mydf)
#> 'data.frame':	20 obs. of  7 variables:
#>  $ c12hour : num  16 148 70 168 168 16 161 110 28 40 ...
#>   ..- attr(*, "label")= chr "average number of hours of care per week"
#>  $ e15relat: num  2 2 1 1 2 2 1 4 2 2 ...
#>   ..- attr(*, "labels")= Named num [1:8] 1 2 3 4 5 6 7 8
#>   .. ..- attr(*, "names")= chr [1:8] "spouse/partner" "child" "sibling" "daughter or son -in-law" ...
#>   ..- attr(*, "label")= chr "relationship to elder"
#>  $ e16sex  : num  2 2 2 2 2 2 1 2 2 2 ...
#>   ..- attr(*, "labels")= Named num [1:2] 1 2
#>   .. ..- attr(*, "names")= chr [1:2] "male" "female"
#>   ..- attr(*, "label")= chr "elder's gender"
#>  $ e17age  : num  83 88 82 67 84 85 74 87 79 83 ...
#>   ..- attr(*, "label")= chr "elder' age"
#>  $ e42dep  : num  3 3 3 4 4 4 4 4 4 4 ...
#>   ..- attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   .. ..- attr(*, "names")= chr [1:4] "independent" "slightly dependent" "moderately dependent" "severely dependent"
#>   ..- attr(*, "label")= chr "elder's dependency"
#>  $ c82cop1 : num  NA NA NA NA NA NA NA NA NA NA ...
#>   ..- attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   .. ..- attr(*, "names")= chr [1:4] "never" "sometimes" "often" "always"
#>   ..- attr(*, "label")= chr "do you feel you cope well as caregiver?"
#>  $ c83cop2 : num  NA NA NA NA NA NA NA NA NA NA ...
#>   ..- attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   .. ..- attr(*, "names")= chr [1:4] "Never" "Sometimes" "Often" "Always"
#>   ..- attr(*, "label")= chr "do you find caregiving too demanding?"

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
#>    subsets c12hour e15relat e16sex e17age e42dep c82cop1 c83cop2 c84cop3
#> 1        1      16        2      2     83      3      NA      NA      NA
#> 2        1     148        2      2     88      3      NA      NA      NA
#> 3        1      70        1      2     82      3      NA      NA      NA
#> 4        1     168        1      2     67      4      NA      NA      NA
#> 5        1     168        2      2     84      4      NA      NA      NA
#> 6        1      16        2      2     85      4      NA      NA      NA
#> 7        1     161        1      1     74      4      NA      NA      NA
#> 8        1     110        4      2     87      4      NA      NA      NA
#> 9        1      28        2      2     79      4      NA      NA      NA
#> 10       1      40        2      2     83      4      NA      NA      NA
#> 11       2      NA       NA      1     68      4       3       4      NA
#> 12       2      NA       NA      2     97      3       3       3      NA
#> 13       2      NA       NA      2     80      4       3       2      NA
#> 14       2      NA       NA      2     75      3       3       2      NA
#> 15       2      NA       NA      2     82      3       2       3      NA
#> 16       2      NA       NA      2     89      3       4       2      NA
#> 17       2      NA       NA      1     80      1       3       2      NA
#> 18       2      NA       NA      1     72      3       4       2      NA
#> 19       2      NA       NA      1     94      3       3       2      NA
#> 20       2      NA       NA      1     79      4       3       2      NA
#> 21       3      NA       NA     NA     NA      4       3       2       3
#> 22       3      NA       NA     NA     NA      3       4       1       2
#> 23       3      NA       NA     NA     NA      4       3       1       1
#> 24       3      NA       NA     NA     NA      4       3       2       2
#> 25       3      NA       NA     NA     NA      3       3       2       1
#> 26       3      NA       NA     NA     NA      4       2       3       4
#> 27       3      NA       NA     NA     NA      4       2       3       3
#> 28       3      NA       NA     NA     NA      2       2       2       2
#> 29       3      NA       NA     NA     NA      3       3       2       2
#> 30       3      NA       NA     NA     NA      4       2       2       1
#> 31       4      NA       NA     NA     NA     NA      NA      NA      NA
#> 32       4      NA       NA     NA     NA     NA      NA      NA      NA
#> 33       4      NA       NA     NA     NA     NA      NA      NA      NA
#> 34       4      NA       NA     NA     NA     NA      NA      NA      NA
#> 35       4      NA       NA     NA     NA     NA      NA      NA      NA
#> 36       4      NA       NA     NA     NA     NA      NA      NA      NA
#> 37       4      NA       NA     NA     NA     NA      NA      NA      NA
#> 38       4      NA       NA     NA     NA     NA      NA      NA      NA
#> 39       4      NA       NA     NA     NA     NA      NA      NA      NA
#> 40       4      NA       NA     NA     NA     NA      NA      NA      NA
#>    c85cop4 c87cop6 c88cop7 c89cop8 c90cop9
#> 1       NA      NA      NA      NA      NA
#> 2       NA      NA      NA      NA      NA
#> 3       NA      NA      NA      NA      NA
#> 4       NA      NA      NA      NA      NA
#> 5       NA      NA      NA      NA      NA
#> 6       NA      NA      NA      NA      NA
#> 7       NA      NA      NA      NA      NA
#> 8       NA      NA      NA      NA      NA
#> 9       NA      NA      NA      NA      NA
#> 10      NA      NA      NA      NA      NA
#> 11      NA      NA      NA      NA      NA
#> 12      NA      NA      NA      NA      NA
#> 13      NA      NA      NA      NA      NA
#> 14      NA      NA      NA      NA      NA
#> 15      NA      NA      NA      NA      NA
#> 16      NA      NA      NA      NA      NA
#> 17      NA      NA      NA      NA      NA
#> 18      NA      NA      NA      NA      NA
#> 19      NA      NA      NA      NA      NA
#> 20      NA      NA      NA      NA      NA
#> 21       2      NA      NA      NA      NA
#> 22       1      NA      NA      NA      NA
#> 23       1      NA      NA      NA      NA
#> 24       4      NA      NA      NA      NA
#> 25       2      NA      NA      NA      NA
#> 26       4      NA      NA      NA      NA
#> 27      NA      NA      NA      NA      NA
#> 28       2      NA      NA      NA      NA
#> 29       2      NA      NA      NA      NA
#> 30       2      NA      NA      NA      NA
#> 31      NA       4       4       1       3
#> 32      NA       1       1       1       4
#> 33      NA       1       3       1       1
#> 34      NA       1       4       2       3
#> 35      NA       1       1       3       4
#> 36      NA       1       1       4       4
#> 37      NA       1       4       1       1
#> 38      NA       1       1       4       4
#> 39      NA       3       2       1       4
#> 40      NA       1       2       3       3
str(mydf)
#> 'data.frame':	40 obs. of  14 variables:
#>  $ subsets : chr  "1" "1" "1" "1" ...
#>  $ c12hour : num  16 148 70 168 168 16 161 110 28 40 ...
#>   ..- attr(*, "label")= chr "average number of hours of care per week"
#>  $ e15relat: num  2 2 1 1 2 2 1 4 2 2 ...
#>   ..- attr(*, "labels")= Named num [1:8] 1 2 3 4 5 6 7 8
#>   .. ..- attr(*, "names")= chr [1:8] "spouse/partner" "child" "sibling" "daughter or son -in-law" ...
#>   ..- attr(*, "label")= chr "relationship to elder"
#>  $ e16sex  : num  2 2 2 2 2 2 1 2 2 2 ...
#>   ..- attr(*, "labels")= Named num [1:2] 1 2
#>   .. ..- attr(*, "names")= chr [1:2] "male" "female"
#>   ..- attr(*, "label")= chr "elder's gender"
#>  $ e17age  : num  83 88 82 67 84 85 74 87 79 83 ...
#>   ..- attr(*, "label")= chr "elder' age"
#>  $ e42dep  : num  3 3 3 4 4 4 4 4 4 4 ...
#>   ..- attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   .. ..- attr(*, "names")= chr [1:4] "independent" "slightly dependent" "moderately dependent" "severely dependent"
#>   ..- attr(*, "label")= chr "elder's dependency"
#>  $ c82cop1 : num  NA NA NA NA NA NA NA NA NA NA ...
#>   ..- attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   .. ..- attr(*, "names")= chr [1:4] "never" "sometimes" "often" "always"
#>   ..- attr(*, "label")= chr "do you feel you cope well as caregiver?"
#>  $ c83cop2 : num  NA NA NA NA NA NA NA NA NA NA ...
#>   ..- attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   .. ..- attr(*, "names")= chr [1:4] "Never" "Sometimes" "Often" "Always"
#>   ..- attr(*, "label")= chr "do you find caregiving too demanding?"
#>  $ c84cop3 : num  NA NA NA NA NA NA NA NA NA NA ...
#>   ..- attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   .. ..- attr(*, "names")= chr [1:4] "Never" "Sometimes" "Often" "Always"
#>   ..- attr(*, "label")= chr "does caregiving cause difficulties in your relationship with your friends?"
#>  $ c85cop4 : num  NA NA NA NA NA NA NA NA NA NA ...
#>   ..- attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   .. ..- attr(*, "names")= chr [1:4] "Never" "Sometimes" "Often" "Always"
#>   ..- attr(*, "label")= chr "does caregiving have negative effect on your physical health?"
#>  $ c87cop6 : num  NA NA NA NA NA NA NA NA NA NA ...
#>   ..- attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   .. ..- attr(*, "names")= chr [1:4] "Never" "Sometimes" "Often" "Always"
#>   ..- attr(*, "label")= chr "does caregiving cause financial difficulties?"
#>  $ c88cop7 : num  NA NA NA NA NA NA NA NA NA NA ...
#>   ..- attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   .. ..- attr(*, "names")= chr [1:4] "Never" "Sometimes" "Often" "Always"
#>   ..- attr(*, "label")= chr "do you feel trapped in your role as caregiver?"
#>  $ c89cop8 : num  NA NA NA NA NA NA NA NA NA NA ...
#>   ..- attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   .. ..- attr(*, "names")= chr [1:4] "never" "sometimes" "often" "always"
#>   ..- attr(*, "label")= chr "do you feel supported by friends/neighbours?"
#>  $ c90cop9 : num  NA NA NA NA NA NA NA NA NA NA ...
#>   ..- attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   .. ..- attr(*, "names")= chr [1:4] "never" "sometimes" "often" "always"
#>   ..- attr(*, "label")= chr "do you feel caregiving worthwhile?"