Package: labelled


Function: copy_labels_from()


1. Copy variable, value, and missing labels from another dataset that has all the same variables

Review the new data (d5)

# A tibble: 3 x 3
  Var1   Var2  Var3
  <chr> <dbl> <dbl>
1 d         1    NA
2 e         2     2
3 f         1     1

Review the labelled dataset with the same variables (d4)

# A tibble: 3 x 3
  Var1   Var2  Var3
  <chr> <dbl> <dbl>
1 a         1     2
2 b        NA     3
3 c         3     1

View the missing labels for the new data (d5)

labelled::var_label(d5)
$Var1
NULL

$Var2
NULL

$Var3
NULL

View the labels for the labelled data (d4)

labelled::var_label(d4)
$Var1
[1] "Name"

$Var2
[1] "Interest in Homework"

$Var3
[1] "Interest in School"

Copy labels from d4 to d5

d5 <- d5 %>% 
  labelled::copy_labels_from(d4)

d5 %>% 
  labelled::var_label()
$Var1
[1] "Name"

$Var2
[1] "Interest in Homework"

$Var3
[1] "Interest in School"

2. Copy variable, value, and missing labels from another dataset that only has some of the same variables

labelled::copy_labels_from() also works if not all of the same variables exist in both datasets

Review the new dataset d6 that has some of the same variables as d4 but with no labels

# A tibble: 3 x 3
   Var2  Var3  Var4
  <dbl> <dbl> <dbl>
1     2     1     5
2     3     2     4
3     1     2     3

Review the labelled dataset with some of the same variables (d4)

# A tibble: 3 x 3
  Var1   Var2  Var3
  <chr> <dbl> <dbl>
1 a         1     2
2 b        NA     3
3 c         3     1

View the missing labels for d6

labelled::var_label(d6)
$Var2
NULL

$Var3
NULL

$Var4
NULL

View the labels for the labelled data (d4)

labelled::var_label(d4)
$Var1
[1] "Name"

$Var2
[1] "Interest in Homework"

$Var3
[1] "Interest in School"

Copy labels from d4 to d6

d6 <- d6 %>% 
  labelled::copy_labels_from(d4)

d6 %>% 
  labelled::var_label()
$Var2
[1] "Interest in Homework"

$Var3
[1] "Interest in School"

$Var4
NULL

Function: copy_labels()


Note: With this function, the dataset you are copying from goes first.


1. Similar to labelled::copy_labels_from, labelled::copy_labels copies labels from one dataset to another

Review the new data (d5)

# A tibble: 3 x 3
  Var1   Var2  Var3
  <chr> <dbl> <dbl>
1 d         1    NA
2 e         2     2
3 f         1     1

Review the labelled dataset with the same variables (d4)

# A tibble: 3 x 3
  Var1   Var2  Var3
  <chr> <dbl> <dbl>
1 a         1     2
2 b        NA     3
3 c         3     1

View the missing labels for the new data (d5)

labelled::var_label(d5)
$Var1
NULL

$Var2
NULL

$Var3
NULL

View the labels for the labelled data (d4)

labelled::var_label(d4)
$Var1
[1] "Name"

$Var2
[1] "Interest in Homework"

$Var3
[1] "Interest in School"

Copy labels from d4 to d5

d5 <- labelled::copy_labels(d4, d5)

d5 %>% 
  labelled::var_label()
$Var1
[1] "Name"

$Var2
[1] "Interest in Homework"

$Var3
[1] "Interest in School"

Return to Label Data