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

Posted

技术标签:

【中文标题】在 expss 方法 apply_label 的参数处使用列表【英文标题】:Use a list at the argument for expss method apply_label 【发布时间】:2021-12-20 05:09:18 【问题描述】:

我想在包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

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

看起来 mtcars1

以上是关于在 expss 方法 apply_label 的参数处使用列表的主要内容,如果未能解决你的问题,请参考以下文章

使用字符串变量在 R 函数中传递多个变量信息

写为Stata文件时,R中应用的标签不保存

使用 expss 包调整列宽

使用 expss 在 R Marksdown 中格式化表格

使用用户定义的函数在 Expss 表中动态传递参数

在单个 expss 表中添加和堆叠子组