Search code examples
rdataframelabel

R: How get the labels of attributes in a list


I have a data frame with labeled columns meaning I have both column names and one label for each column. The following code creates an analogous data frame:

data(mtcars)
mtcars = apply_labels(mtcars,
                      mpg = "Miles/(US) gallon",
                      cyl = "Number of cylinders",
                      disc = "Displacement (cu. in.)",
                      hp = "Gross horsepower",
                      drat = "Rear axle ratio",
                      wt = "weight (1000 lbs)",
                      qsec = "1/4 mile time",
                      vs = "Engine",
                      am = "Transmission"
                      gear = "Number of forward gears"
                      carb = "Number of carburetors"

My goal is to select columns by their labels ("Miles/(US) gallon", etc.). Does anyone know how to get access to those or know a function returning those labels?

Thank you very much for your help.


Solution

  • I assume you are using the expss package to apply the labels. This package also contains the function var_lab which returns the label of any column. You can get a vector of labels using sapply(mtcars, var_lab). Then you can use this vector to subset the columns you need from the data frame.

    library(expss)
    data(mtcars)
    
    mtcars = apply_labels(mtcars,
                          mpg = "Miles/(US) gallon",
                          cyl = "Number of cylinders",
                          disp = "Displacement (cu. in.)",
                          hp = "Gross horsepower",
                          drat = "Rear axle ratio",
                          wt = "weight (1000 lbs)",
                          qsec = "1/4 mile time",
                          vs = "Engine",
                          am = "Transmission",
                          gear = "Number of forward gears",
                          carb = "Number of carburetors")
    
    
    labs <- sapply(mtcars,var_lab)
    
    print(labs)
    ##                       mpg                       cyl                      disp 
    ##       "Miles/(US) gallon"     "Number of cylinders"  "Displacement (cu. in.)" 
    ##                        hp                      drat                        wt 
    ##        "Gross horsepower"         "Rear axle ratio"       "weight (1000 lbs)" 
    ##                      qsec                        vs                        am 
    ##           "1/4 mile time"                  "Engine"            "Transmission" 
    ##                      gear                      carb 
    ## "Number of forward gears"   "Number of carburetors"
    
    subset <- mtcars[,labs %in% c('Number of cylinders','Gross horsepower')]
    
    head(subset)
    ##                   cyl  hp
    ## Mazda RX4           6 110
    ## Mazda RX4 Wag       6 110
    ## Datsun 710          4  93
    ## Hornet 4 Drive      6 110
    ## Hornet Sportabout   8 175
    ## Valiant             6 105