gpt4 book ai didi

R expss use_labels 和 dplyr 逻辑

转载 作者:行者123 更新时间:2023-12-04 10:55:48 24 4
gpt4 key购买 nike

我试图让 expss use_labels 与 dplyr 逻辑一起工作 - 请参见下面的示例。

小插图在 use_labels 下说明了以下内容。
到目前为止,变量标签支持仅适用于将在 data.frame 中计算的表达式。
这是我在这里遇到的问题吗?

#

##########################################
library(expss)
library(tidyverse)
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"
)

# table with caption from label - labels working
cro_cpct(mtcars$am, mtcars$vs) %>% set_caption(var_lab(mtcars$am))

## This works as expected - now to get this with expss use_labels.
mtcars %>%
group_by(am) %>%
summarise(
freq = n()
)
#######
#am freq
#<labelled> <int>
# 1 0 19
# 2 1 13
########################

#### This doesn't work - i.e. not labelled
use_labels(mtcars %>%
group_by(am) %>%
summarise(
freq = n()
))
## Error in substitute_symbols(expr, c(substitution_list, list(..data = quote(expss::vars(other))))) :
# argument "expr" is missing, with no default

如果标签不能与 dplyr 逻辑一起使用,有人知道另一个可以用 dplyr 做标签的包吗?
问候

最佳答案

您可以使用 ...data用于访问表达式和 values2labels 中的数据的参数(感谢@Gregory Demin)获取标签。

library(expss)  
use_labels(mtcars, ..data %>%
group_by(am) %>%
summarise(freq = n()) %>% values2labels)
# A tibble: 2 x 2
# Transmission freq
# <labelled> <int>
#1 Automatic 19
#2 Manual 13

关于R expss use_labels 和 dplyr 逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59208121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com