Home > Enterprise >  Use a list at the argument for expss method apply_label
Use a list at the argument for expss method apply_label

Time:11-07

I want to use a list as the argument for apply_labels in the package expss

This works from the vignette:

library(expss)
#> Warning: package 'expss' was built under R version 4.1.1
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",
                      vs = c("V-engine" = 0,
                             "Straight engine" = 1),
                      am = "Transmission",
                      am = c("Automatic" = 0,
                             "Manual"=1),
                      gear = "Number of forward gears",
                      carb = "Number of carburetors"
)
mtcars %>% str
#> 'data.frame':    32 obs. of  11 variables:
#>  $ mpg :Class 'labelled' num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
#>    .. .. LABEL: Miles/(US) gallon 
#>  $ cyl :Class 'labelled' num  6 6 4 6 8 6 8 4 4 6 ...
#>    .. .. LABEL: Number of cylinders 
#>  $ disp:Class 'labelled' num  160 160 108 258 360 ...
#>    .. .. LAB

This is my attempt using a list:

library(expss)
#> Warning: package 'expss' was built under R version 4.1.1
#> 
#> Use 'expss_output_viewer()' to display tables in the RStudio Viewer.
#>  To return to the console output, use 'expss_output_default()'.
data(mtcars)
new_labels <- list(
                       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",
                      vs = c("V-engine" = 0,
                             "Straight engine" = 1),
                      am = "Transmission",
                      am = c("Automatic" = 0,
                             "Manual"=1),
                      gear = "Number of forward gears",
                      carb = "Number of carburetors" 
)
mtcars = apply_labels(mtcars, new_labels  )
#> Error in if (curr_name %in% data_names) {: argument is of length zero

CodePudding user response:

You need to use a specific R idiom do.call. Last statement will look like this:

mtcars = do.call(apply_labels, c(list(mtcars), new_labels))
  • Related