Search code examples
rspssexpss

How to use R character vector element as string and variable inside function?


I am trying to apply SPSS style category labels to my dataset in R. I think my question arises as I do not know how to parse variables correctly, so is not necessarily related to just these types of data. To begin with, doing this manually as per the expss library documentation works fine:

library(expss)

#Load in the data
data(mtcars)

#Apply Variable Labels and Value Labels (and Numeric Coding) to each Variable.
mtcars = apply_labels(mtcars,
                      vs = "Engine",
                      vs = c("V-engine" = 1,
                             "Straight engine" = 2,
                             "Other engine" = 3)
)

Now my problem arises if I have my "Variable Names", "Variable Labels", "Value Labels" and corresponding "Value Numeric Codes" stored in some R data type and I try to use them in the apply_labels function. For example, if I have these stored in character vectors like so:

#Load in the data
data(mtcars)

#Value Labels
value_lab<-c("V-engine","Straight engine","Other engine")
#Value's Numeric coding
value_num<-c("1","2","3")

#Variable names
var <- c("vs")
#Variable Labels
var_lab<-c("Engine")

Then my question is, how would I use my character vector elements inside the apply_labels function? e.g. how would I do something like this:

#Apply Variable Labels and Value Labels (and Numeric Coding) to each Variable.
mtcars = apply_labels(mtcars,
                      var[1] = var_lab[1],
                      var[1] = c(value_lab[1] = value_num[1],
                                 value_lab[2] = value_num[2],
                                 value_lab[3] = value_num[3])
)

I have tried various combinations of paste and toString without success. My next step will be to apply this to my 500,000+ rows x 20,000 columns of data with a to-be-determined number of possible Value Labels/Numeric Codings. Obligatory: I am new to R. Thank you.


Solution

  • To achieve your desired result

    1. Make use of named lists and vectors to store your variable and value labels
    2. Doing so you can make use of do.call to pass the variable and value labels to apply_labels

    To make the example more interesting I added labels for a second variable.

    library(expss)
    
    # Variable Labels
    var_labels <- list(vs = "Engine", am = "Transmission")
    #Value Labels
    val_labels <- list(
      vs = c("V-engine" = 0, "Straight engine" = 1),
      am = c("Automatic" = 0, "Manual" = 1)
    )
    
    mtcars2 <- do.call(apply_labels, c(list(data = mtcars), var_labels, val_labels))
    
    table(mtcars2$am, mtcars2$vs)
    #>            
    #>             V-engine Straight engine
    #>   Automatic       12               7
    #>   Manual           6               7