R - change values in labelled column of dataframe without losing all labels in column

Viewed 16

I'm trying to learn how to work with the sjlabelled package in R, and labelled data more generally. I'm trying

Here's the output for what I tried for a very simple example:

> library(dplyr)
> library(sjlabelled)
> 
> df <- data.frame(col1 = c(1:3, 1:3),
+                  col2 = c(seq(11, 33, 11), 11, 12, 13))
> 
> df <- df %>%
+   set_labels(col1, labels = c("a" = 1, "b" = 2, "c" = 3)) %>%
+   set_labels(col2, labels = c("A" = 11, "B" = 12, "C" = 13, "D" = 22, "E" = 33))
> 
> df
  col1 col2
1    1   11
2    2   22
3    3   33
4    1   11
5    2   12
6    3   13
> 
> get_labels(df)
$col1
[1] "a" "b" "c"

$col2
[1] "A" "B" "C" "D" "E"

> 
> df <- df %>%
+   mutate(col1 = ifelse(col1 > 1 & col2 < 33, 2, as_labelled(col1)))
> 
> df
  col1 col2
1    1   11
2    2   22
3    3   33
4    1   11
5    2   12
6    2   13
> 
> get_labels(df)
$col1
NULL

$col2
[1] "A" "B" "C" "D" "E"

I have used as_labelled to preserve labels in other situations, such as when using rbind for data frames with labelled data, but it isn't working here.

Are either of the following possible using sjlabelled or a similar approach:

a) overwriting values with other values which had already been assigned a label so that labels are preserved and the overwritten value has its label overwritten with the corresponding label (e.g. any values that are overwritten as '2' in the example will now be labelled as "b")? b) overwriting values with values which weren't in the column (e.g. NA) without losing the labels from the values which were already labelled (e.g. with the example, overwriting '1', '2' and '3', with NA but keeping '4' labelled as "d")?

Thank you.

1 Answers

We could assign as

df$col1[with(df, col1 > 1 & col2 < 33)] <- 2

-checking

> get_labels(df)
$col1
[1] "a" "b" "c"

$col2
[1] "A" "B" "C" "D" "E"

> df
  col1 col2
1    1   11
2    2   22
3    3   33
4    1   11
5    2   12
6    2   13
Related