Skip to contents

This function takes a data frame as input and returns a character vector of the names of the variables in the data frame that are labelled using the `haven` package.

Usage

which_labelled(data)

Arguments

data

A data frame to check for labelled variables.

Value

A character vector of the names of the labelled variables in the input data frame.

Examples

library(haven)
df <- data.frame(
  HHID = c(rep("hh01",5),rep("hh02",5)),
  food_item = (rep(101:105,2)),
  consYN = sample(1:2,10,replace = TRUE)
) 

# Add value labels
df$food_item <- labelled(df$food_item, c("maize" = 101, "wheat" = 102, 
"rice" = 103, "meat" = 104, "fish" = 105))
df$consYN <- labelled(df$consYN, c("Yes" = 1, "No" = 2))

# Print data frame
df
#>    HHID food_item consYN
#> 1  hh01       101      2
#> 2  hh01       102      1
#> 3  hh01       103      1
#> 4  hh01       104      1
#> 5  hh01       105      1
#> 6  hh02       101      2
#> 7  hh02       102      1
#> 8  hh02       103      1
#> 9  hh02       104      2
#> 10 hh02       105      2

# Check for labelled variables
which_labelled(df)
#> The following variables are labelled: Use `hcesNutR::split_dta()` to split the data into two separate columns.
#> [1] "food_item" "consYN"