首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在expss方法apply_label的参数处使用列表

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

Stack Overflow用户
提问于 2021-11-06 15:12:21
回答 1查看 36关注 0票数 1

我想使用一个列表作为expss包中apply_labels的参数

这是从小插曲开始的:

代码语言:javascript
复制
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

这是我使用列表的尝试:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-06 22:23:03

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

代码语言:javascript
复制
mtcars = do.call(apply_labels, c(list(mtcars), new_labels))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69865206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档