Home > other >  Label observations across multiple columns in R using purrr
Label observations across multiple columns in R using purrr

Time:04-08

I have a list of observation labels that goes with a .csv data set, e.g., 1 = "Yes", 0 = "No", where the label for all observations equal to 1 for column k has the label of "Yes" but retains the underlying numerical value of 1 or 0.

I want to iteratively add labels to my dataset using this list, ideally using purrr or the tidyverse.

Here is an example of what I am trying to do, but at scale across multiple columns.

# load packages
library(tidyverse)
library(sjlabelled)

# load package data
data(efc)

# let's just start with a basic data frame
efc_twovar <- efc %>% select(e42dep, e15relat)

# create a list of labels
lab_list <- list(attr(efc$e42dep, "labels"), attr(efc$e15relat, "labels"))

# remove the labels from our data set
attr(efc_twovar, "labels") <- ""

# basic set up
set_labels(efc_twovar$e42dep, labels =  lab_list[1])

# purrr attempt
map(efc_twovar, set_labels, labels = lab_list)

The purrr attempt above results in the labels from the first vector in the list getting applied to both columns in the data frame rather than just the first. I ultimately want to do this with a list of about 20 label vectors for as many columns.

CodePudding user response:

One option to achieve your desired result would be to name your labels list according to the column names of your data. Doing so you could label your data using just set_labels:

library(sjlabelled)
data(efc)

efc_twovar <- efc[c("e42dep", "e15relat")]
attr(efc_twovar, "labels") <- NULL

lab_list <- list(attr(efc$e42dep, "labels"), attr(efc$e15relat, "labels"))
names(lab_list) <- names(efc_twovar)

efc_twovar <- set_labels(efc_twovar, labels =  lab_list)

lapply(efc_twovar, attributes)
#> $e42dep
#> $e42dep$label
#> [1] "elder's dependency"
#> 
#> $e42dep$labels
#>          independent   slightly dependent moderately dependent 
#>                    1                    2                    3 
#>   severely dependent 
#>                    4 
#> 
#> 
#> $e15relat
#> $e15relat$label
#> [1] "relationship to elder"
#> 
#> $e15relat$labels
#>          spouse/partner                   child                 sibling 
#>                       1                       2                       3 
#> daughter or son -in-law              ancle/aunt            nephew/niece 
#>                       4                       5                       6 
#>                  cousin          other, specify 
#>                       7                       8
  • Related