library(dplyr)
df <- tibble(
id = 1:3,
happy = factor(c('yes', 'no', 'yes')),
gender = labelled(c(1, 1, 2), c(female = 1, male = 2))
) %>%
set_variable_labels(
id = "Individual ID",
happy = "Are you happy?",
gender = "Gender of respondent"
)
var_label(df)
fdf <- df %>% filter(id < 3)
var_label(fdf) # some variable labels have been lost
fdf <- fdf %>% copy_labels_from(df)
var_label(fdf)
# Alternative syntax
fdf <- subset(df, id < 3)
fdf <- copy_labels(from = df, to = fdf)
Run the code above in your browser using DataLab