Skip to contents

This function splits a labelled variable into two new variables: one containing the numeric values and one containing the character labels. The new variables are added to the input data frame and the original variable is dropped by default.

Usage

split_dta(data, split_var, val_to, lab_to, drop_split_var = TRUE)

Arguments

data

A data frame containing the input variable.

split_var

A character string specifying the name of labelled the input variable to split.

val_to

A character string specifying the name of the new variable to contain the numeric values.

lab_to

A character string specifying the name of the new variable to contain the character labels.

drop_split_var

A logical value indicating whether to drop the original variable (default is TRUE).

Value

A data frame with the new variables added and the original variable dropped (if specified).

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      1
#> 2  hh01       102      1
#> 3  hh01       103      2
#> 4  hh01       104      1
#> 5  hh01       105      1
#> 6  hh02       101      2
#> 7  hh02       102      1
#> 8  hh02       103      1
#> 9  hh02       104      1
#> 10 hh02       105      2

# Split the food_item column into two new columns
split_dta(df, "food_item", "food_item_id", "food_item_lab")
#> Variable food_item has been split into food_item_id and food_item_lab.
#> Variable food_item has been dropped.
#>    HHID consYN food_item_id food_item_lab
#> 1  hh01      1          101         maize
#> 2  hh01      1          102         wheat
#> 3  hh01      2          103          rice
#> 4  hh01      1          104          meat
#> 5  hh01      1          105          fish
#> 6  hh02      2          101         maize
#> 7  hh02      1          102         wheat
#> 8  hh02      1          103          rice
#> 9  hh02      1          104          meat
#> 10 hh02      2          105          fish