r - 在 expss 方法 apply_label 的参数处使用列表
问题描述
我想使用列表作为apply_labels
包中的参数expss
这适用于小插图:
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 idiom do.call
。最后一条语句如下所示:
mtcars = do.call(apply_labels, c(list(mtcars), new_labels))
推荐阅读
- typescript - 在另一个属性的断言之后的一个属性的打字稿推断
- excel - VBA 宏代码将多个图表从 Excel 复制到 Words 并将它们保存在不同的文档中
- r - 在 R 中使用 lapply 循环遍历 R 中的 data.frames 列表
- html - 标签在 div 内不可点击
- reactjs - 无法读取 undefined 的属性“事务”。反应原生
- xamarin.forms - Mono Bug:在改变对象成员后尝试观察新分配的值时出现错误消息
- java - 编码问题??????而不是字母
- angular - 处理多个可观察对象并返回一个值
- javascript - 如何在 Angular 组件中显示来自 Observable 的数据
- jenkins - 如何将数组分配给Jenkinsfile中的Env变量