在 expss 方法的参数中使用列表 apply_label

Use a list at the argument for expss method apply_label

我想在包 expss

中使用列表作为 apply_labels 的参数

这来自小插图:

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

这是我使用列表的尝试:

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

您需要使用特定的 R 习语 do.call。最后一条语句将如下所示:

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