Subsetting-functions usually drop value and variable labels from subsetted data frames (if the original data frame has value and variable label attributes). This function copies these value and variable labels back to subsetted data frames that have been subsetted, for instance, with subset.

copy_labels(df_new, df_origin = NULL, ...)

Arguments

df_new

The new, subsetted data frame.

df_origin

The original data frame where the subset (df_new) stems from; use NULL, if value and variable labels from df_new should be removed.

...

Optional, unquoted names of variables that should be selected for further processing. Required, if x is a data frame (and no vector) and only selected variables from x should be processed. You may also use functions like : or tidyselect's select-helpers. See 'Examples'.

Value

Returns df_new with either removed value and variable label attributes (if df_origin = NULL) or with copied value and variable label attributes (if df_origin was the original subsetted data frame).

Note

In case df_origin = NULL, all possible label attributes from df_new are removed.

Examples

data(efc)

# create subset - drops label attributes
efc.sub <- subset(efc, subset = e16sex == 1, select = c(4:8))
str(efc.sub)
#> 'data.frame':	296 obs. of  5 variables:
#>  $ e17age : num  74 68 80 72 94 79 67 80 76 88 ...
#>  $ e42dep : num  4 4 1 3 3 4 3 4 2 4 ...
#>  $ c82cop1: num  4 3 3 4 3 3 4 2 2 3 ...
#>  $ c83cop2: num  2 4 2 2 2 2 1 3 2 2 ...
#>  $ c84cop3: num  4 4 1 1 1 4 2 4 2 4 ...

# copy back attributes from original dataframe
efc.sub <- copy_labels(efc.sub, efc)
str(efc.sub)
#> 'data.frame':	296 obs. of  5 variables:
#>  $ e17age : num  74 68 80 72 94 79 67 80 76 88 ...
#>   ..- attr(*, "label")= chr "elder' age"
#>  $ e42dep : num  4 4 1 3 3 4 3 4 2 4 ...
#>   ..- attr(*, "label")= chr "elder's dependency"
#>   ..- attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   .. ..- attr(*, "names")= chr [1:4] "independent" "slightly dependent" "moderately dependent" "severely dependent"
#>  $ c82cop1: num  4 3 3 4 3 3 4 2 2 3 ...
#>   ..- attr(*, "label")= chr "do you feel you cope well as caregiver?"
#>   ..- attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   .. ..- attr(*, "names")= chr [1:4] "never" "sometimes" "often" "always"
#>  $ c83cop2: num  2 4 2 2 2 2 1 3 2 2 ...
#>   ..- attr(*, "label")= chr "do you find caregiving too demanding?"
#>   ..- attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   .. ..- attr(*, "names")= chr [1:4] "Never" "Sometimes" "Often" "Always"
#>  $ c84cop3: num  4 4 1 1 1 4 2 4 2 4 ...
#>   ..- attr(*, "label")= chr "does caregiving cause difficulties in your relationship with your friends?"
#>   ..- attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   .. ..- attr(*, "names")= chr [1:4] "Never" "Sometimes" "Often" "Always"

# remove all labels
efc.sub <- copy_labels(efc.sub)
#> Removing all variable and value labels from data frame.
str(efc.sub)
#> 'data.frame':	296 obs. of  5 variables:
#>  $ e17age : num  74 68 80 72 94 79 67 80 76 88 ...
#>  $ e42dep : num  4 4 1 3 3 4 3 4 2 4 ...
#>  $ c82cop1: num  4 3 3 4 3 3 4 2 2 3 ...
#>  $ c83cop2: num  2 4 2 2 2 2 1 3 2 2 ...
#>  $ c84cop3: num  4 4 1 1 1 4 2 4 2 4 ...

# create subset - drops label attributes
efc.sub <- subset(efc, subset = e16sex == 1, select = c(4:8))
if (require("dplyr")) {
  # create subset with dplyr's select - attributes are preserved
  efc.sub2 <- select(efc, c160age, e42dep, neg_c_7, c82cop1, c84cop3)
  # copy labels from those columns that are available
  copy_labels(efc.sub, efc.sub2) %>% str()
}
#> 'data.frame':	296 obs. of  5 variables:
#>  $ e17age : num  74 68 80 72 94 79 67 80 76 88 ...
#>  $ e42dep : num  4 4 1 3 3 4 3 4 2 4 ...
#>   ..- attr(*, "label")= chr "elder's dependency"
#>   ..- attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   .. ..- attr(*, "names")= chr [1:4] "independent" "slightly dependent" "moderately dependent" "severely dependent"
#>  $ c82cop1: num  4 3 3 4 3 3 4 2 2 3 ...
#>   ..- attr(*, "label")= chr "do you feel you cope well as caregiver?"
#>   ..- attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   .. ..- attr(*, "names")= chr [1:4] "never" "sometimes" "often" "always"
#>  $ c83cop2: num  2 4 2 2 2 2 1 3 2 2 ...
#>  $ c84cop3: num  4 4 1 1 1 4 2 4 2 4 ...
#>   ..- attr(*, "label")= chr "does caregiving cause difficulties in your relationship with your friends?"
#>   ..- attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   .. ..- attr(*, "names")= chr [1:4] "Never" "Sometimes" "Often" "Always"

# copy labels from only some columns
str(copy_labels(efc.sub, efc, e42dep))
#> 'data.frame':	296 obs. of  5 variables:
#>  $ e17age : num  74 68 80 72 94 79 67 80 76 88 ...
#>  $ e42dep : num  4 4 1 3 3 4 3 4 2 4 ...
#>   ..- attr(*, "label")= chr "elder's dependency"
#>   ..- attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   .. ..- attr(*, "names")= chr [1:4] "independent" "slightly dependent" "moderately dependent" "severely dependent"
#>  $ c82cop1: num  4 3 3 4 3 3 4 2 2 3 ...
#>  $ c83cop2: num  2 4 2 2 2 2 1 3 2 2 ...
#>  $ c84cop3: num  4 4 1 1 1 4 2 4 2 4 ...
str(copy_labels(efc.sub, efc, -e17age))
#> 1 variables were not found in the dataset: -e17age
#> 'data.frame':	296 obs. of  5 variables:
#>  $ e17age : num  74 68 80 72 94 79 67 80 76 88 ...
#>  $ e42dep : num  4 4 1 3 3 4 3 4 2 4 ...
#>  $ c82cop1: num  4 3 3 4 3 3 4 2 2 3 ...
#>  $ c83cop2: num  2 4 2 2 2 2 1 3 2 2 ...
#>  $ c84cop3: num  4 4 1 1 1 4 2 4 2 4 ...