1

When I dplyr::filter a data.frame with labels, the labels remain but not for variables of class difftime:

library(tidyverse)
library(sjlabelled)
df <- data.frame(a = c(1, 2, 3),
                 start = c("2009-10-16",
                           "2009-08-13",
                           "2008-02-01"),
                 end = c("2009-12-17",
                         "2009-12-13",
                         "2008-08-03"))
df$start <- as.Date(df$start, format = "%Y-%m-%d")
df$end <- as.Date(df$end, format = "%Y-%m-%d")
df <- df %>% 
  mutate(days_diff = end - start) %>% 
  sjlabelled::var_labels(
    a = "cat variable",
    start = "start date",
    end = "end date",
    days_diff = "day difference")
df

enter image description here

when we filter:

df2 <- df %>% 
  filter(a == 1)

the label is removed:

enter image description here

I thought this shouldn't happen anymore, see here and here. Is this a bug or am I missing something?

Thanks

user63230
  • 4,095
  • 21
  • 43
  • related: https://stackoverflow.com/questions/65544304/loss-of-the-label-after-using-mutate-and-recode-dplyr-of-a-character-class-var – user63230 May 12 '23 at 14:45

0 Answers0